3.30天梯赛第八题---AI

版权声明:菜鸟一枚~~ 有想法可在下面评论, 转载标明出处即可。 https://blog.csdn.net/KLFTESPACE/article/details/88918686

//3.30天梯赛第八题...用string里的find和replace应该就能解出来..当时没解出来,对replace不怎么熟...晚上重做了下,感觉差不多了...等系统更新题目了在修改吧


#include<iostream>
#include<string.h>
#include<cstdio> 
#include<ctype.h>

using namespace std;

string s1[10] = {" me", "I ", "could you", "can you", " ?", "?", " '"};//先将第一人称换掉,,我估计,,应该不会有can i 的例子... 
int Length[10] = {3, 2, 9, 7, 2, 1, 2};
string s2[10] = {" you", "you ", "I could", "I can", "!", "!", "'"} ;

int main()
{
	int N;
	
	cin >> N;
	getchar();//此处需有 
	
	for(int i=0; i<N; i++){
		string s;
		
		getline(cin, s);
		
		
		cout << s << endl;
		//大写变小写,去掉空格
		
		
		int j = 0, flag = 0;//flag用来标记是否开头连续空格字符 
		 
		for(int i=0; s[i]; i++){
			if(s[i] == ' ' && flag == 0){
				s.erase(i, 1);//删除从i开始的1个字符 
				i --;
			//	cout << s << endl; 
				continue;//开头连续字符为空...跳过 
			} 
			flag = 1; 
			
			if(s[i] == 'I' && s[i+1] == ' '){
				;//I要大写 
			}
			
			else if(s[i] == ' ')
			{
				if(s[i-1] == ' '){
					s.erase(i, 1);
					i--;
				}
				//先去掉连续空格
			}
			
			else s[i] = tolower(s[i]);
		} 
		
		//cout << s << endl;
		/*
		//s1和上面的数组冲突....待修改.... 
		string s1 = "";
		
		int j = 0, flag = 0;//flag用来标记是否开头连续空格字符 
		 
		for(int i=0; s[i]; i++){
			if(s[i] == ' ' && flag == 0){
				continue;//开头连续字符为空...跳过 
			} 
			flag = 1; 
			
			if(s[i] == 'I' && s[i+1] == ' '){
				s1[j++] = s[i];
			}
			else if(s[i] == ' ')
			{
				if(s[i-1] == ' ')
			
				continue;//先去掉连续空格 
				else s[j++] = s[i];
			}
			
			else s1[j++] = tolower(s[i]);
		}
		s1[j] = '\0'; 
		
		cout << s1 << endl;
		
		
		for(int i=0; i<7; i++){
			
			int tmp = s1.find(s1[i]);
			cout << tmp << endl; 
			if(~tmp){
				
				basic_string<char>::iterator ITERF0 = s1.begin()+tmp;
				basic_string<char>::iterator ITERL0 = s1.begin()+tmp+Length[i];
				
				s1 = s1.replace(ITERF0, ITERL0, s2[i]);

			}
		}
		*/
		
		
		for(int i=0; i<7; i++){
			
			int tmp = s.find(s1[i]);
//			cout << tmp << endl; 
			if(~tmp){
				//数组下标 
				s = s.replace(tmp, Length[i], s2[i]);//中间应该是被替换的长度 而不是被替换的结束下标 
				
				
				//迭代器解法中间为结束位置 
/*				basic_string<char>::iterator ITERF0 = s.begin()+tmp;
				basic_string<char>::iterator ITERL0 = s.begin()+tmp+Length[i];
				
				s = s.replace(ITERF0, ITERL0, s2[i]);//s.replace(1,2,”nternationalizatio”);//从索引1开始的2个替换成后面的C_string
*/
			}
		}
		cout << "AI: " << s << endl;
		
	}
	
	
	return 0;
 } 

猜你喜欢

转载自blog.csdn.net/KLFTESPACE/article/details/88918686