L1-064 估值一亿的AI核心代码 (20 分) C++ AC代码

#include<iostream>
#include<string>
using namespace std;
string s;
int check(const char *str,int i,int n){
		if((i==0||s[i-1]>'z' || (s[i-1]<'a' && s[i-1]!='I')) && ((i+n)>=s.length()||s[i+n]>'z' || (s[i+n]<'a' && s[i+n]!='I')))
		return 1;
		return 0;
}//判断字符串是否独立 
int main(){ 
	int t,i;
	cin>>t;
	cin.get();
	while(t--){
		s.clear();
		getline(cin,s);
		cout<<s<<'\n';
		for(i=0;i<s.length();i++){
	    	if(s.at(i)>='A'&&s.at(i)<='Z'&&s.at(i)!='I')
			    s.at(i)+=32;
			if(s.at(i)=='?')
				s.at(i)='!';
			if(i>0&&s.at(i-1)==' '){
				if((s.at(i)>='a'&&s.at(i)<='z')||s.at(i)=='I'||((s.at(i)>='0'&&s.at(i)<='9')))
				continue;
				s.erase(i-1,1);
				i--;
			}//将非字母,数字前的空格删除 
		}
		if(s.at(0)==' ') 
			s.erase(0,1);	//如果第一个字符为空格,删除它 
		if(s.length()!=0&&s.at(s.length()-1)==' ') 
			s.erase(s.length()-1,1); //如果最后一个字符为空格,删除它 
		for(i=0;i<s.length();i++){
			if(s.at(i)=='I'){
				if(check("I",i,1))
					s.replace(i,1,"you") ;
				i+=3;
			}
			else if(s.substr(i,2)=="me"){
				if(check("me",i,2))
					s.replace(i,2,"you") ;
				i+=3;
			}
			else if(s.substr(i,7)=="can you"){
				if(check("can you",i,7))
					s.replace(i,7,"I can") ;
				i+=5;
			}
			else if(s.substr(i,9)=="could you"){
				if(check("could you",i,9))
					s.replace(i,9,"I could") ;
				i+=7;
			}
		}
		cout<<"AI: "<<s<<endl;	
  	}
	return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值