sicily--1252. Defining Moment

本文探讨了一个涉及字符串应用与特定前缀后缀规则的问题,通过代码实例展示了如何处理字符串的前缀和后缀操作,包括去除特定前缀、后缀,并根据条件进行字符串的变形操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.这题在网上的分类中属于排序部分,但是我怎么做都觉得这就是考 string 的应用

2.坑爹的地方是前缀"re" 所对应的翻译是要在"word"的后方; 后缀"er" 所对应的反应要在原有的"word"的基础上在屁股加"s";“tion” 后要加"ing"

3.最终输出的"word"是去除了前缀跟后缀之后剩下的,题目已经说明了去除之后不会为空的

4.前缀一定是要从第0下标开始; 后缀一定是要从屁股数起

5.先判断前缀,后判断后缀; 而且最多只能有一个前缀跟后缀

6.很无聊的一道题,不过让我练了练string 的函数

#include<iostream>
#include<string>
using namespace std;

int main()
{
	int testNum;
	cin >> testNum;
	while(testNum--)
	{
		string word;
		string prefix, suffix;
		cin >> word;
		//只找一个前缀
		if(word.find("anti") == 0){//能找到"anti"子字符串
			prefix = "against ";
			word.erase(0, 4);//从下标0开始往后删除4个字符
		}
		else if(word.find("post") == 0){
			prefix = "after ";
			word.erase(0, 4);
		}
		else if(word.find("pre") == 0){
			prefix = "before ";
			word.erase(0, 3);
		}
		else if(word.find("re") == 0){
			prefix = " again";
			word.erase(0, 2);
		}
		else if(word.find("un") == 0){
			prefix = "not ";
			word.erase(0, 2);
		}

		if( word.size() > 2 && word.substr( word.size() - 2 ) == "er" ){
			word.erase(word.size() - 2 , 2);//从找到"er"的下标开始删除2个字符
			if(prefix != " again")
				cout << prefix + "one who "  + word + "s" << endl;
			else
				cout << "one who " + word + "s" + prefix << endl;
		}
		else if( word.size() > 3 && word.substr( word.size() - 3 ) == "ing" ){
			word.erase(word.size() - 3, 3);
			if(prefix != " again")
				cout << prefix + "to actively " + word << endl;
			else
				cout << "to actively " + word + prefix << endl;
		}
		else if(word.size() > 3 && word.substr( word.size() - 3 ) == "ize" ){
			word.erase(word.size() - 3, 3);
			if(prefix != " again")
				cout << prefix + "change into " + word << endl;
			else
				cout << "change into " + word + prefix << endl;
		}
		else if(word.size() > 1 && word.substr( word.size() - 1 ) == "s"){
			word.erase(word.size() - 1, 1);
			if(prefix != " again")
				cout << prefix + "multiple instances of " + word << endl;
			else
				cout << "multiple instances of " + word + prefix << endl;
		}
		else if(word.size() > 4 && word.substr( word.size() - 4 ) == "tion" )
		{
			word.erase(word.size() - 4, 4);
			if(prefix != " again")
				cout << prefix + "the process of " + word + "ing" << endl;
			else
				cout << "the process of " + word + "ing" + prefix << endl;
		}
		else//没有后缀
		{
			if(prefix != " again")
				cout << prefix + word << endl;
			else
				cout << word + prefix << endl;
		}

	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值