L1-064 估值一亿的AI核心代码


AI.jpg

以上图片来自新浪微博。

本题要求你实现一个稍微更值钱一点的 AI 英文问答程序,规则是:

  • 无论用户说什么,首先把对方说的话在一行中原样打印出来;
  • 消除原文中多余空格:把相邻单词间的多个空格换成 1 个空格,把行首尾的空格全部删掉,把标点符号前面的空格删掉;
  • 把原文中所有大写英文字母变成小写,除了 I
  • 把原文中所有独立的 can youcould you 对应地换成 I canI could—— 这里“独立”是指被空格或标点符号分隔开的单词;
  • 把原文中所有独立的 I 和 me 换成 you
  • 把原文中所有的问号 ? 换成惊叹号 !
  • 在一行中输出替换后的句子作为 AI 的回答。

输入格式:

输入首先在第一行给出不超过 10 的正整数 N,随后 N 行,每行给出一句不超过 1000 个字符的、以回车结尾的用户的对话,对话为非空字符串,仅包括字母、数字、空格、可见的半角标点符号。

输出格式:

按题面要求输出,每个 AI 的回答前要加上 AI: 和一个空格。

输入样例:

6 Hello ? Good to chat with you can you speak Chinese? Really? Could you show me 5 What Is this prime? I,don 't know 

输出样例:

Hello ? AI: hello! Good to chat with you AI: good to chat with you can you speak Chinese? AI: I can speak chinese! Really? AI: really! Could you show me 5 AI: I could show you 5 What Is this prime? I,don 't know AI: what Is this prime! you,don't know 
 
思路: 这道题浪费我一堆时间最后还没写出来,我佛了,后面补了这道题, 我的写法是在分隔符号之前插入空格,处理之后再根据空格作为独立条件导入vector开的string数组,再处理“can you” or“could you”的问题,最后输出要注意,分隔符号前不能输出空格,判断一遍再输出就好了......
 
 
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<map>
 7 #include<set>
 8 #include<vector>
 9 using namespace std; 10 #define ll long long 
11 #define dd cout<<endl
12 const int inf=99999999; 13 const int mod=1e9+7; 14 const int maxn=1e6+7; 15 string str; 16 vector<string>v; 17 int main() 18 { 19     int T; 20     cin>>T; 21  getchar(); 22     while(T--) 23  { 24  getline(cin,str); 25         cout<<str<<endl<<"AI: "; 26         for(int i=0;i<str.size();i++) 27  { 28             if(str[i]>='A'&&str[i]<='Z')//字母 
29  { 30                 if(str[i]!='I')//'I'不变 
31                     str[i]=str[i]-'A'+'a'; 32  } 33             else if((str[i]>='0'&&str[i]<='9')||(str[i]>='a'&&str[i]<='z'))//数字和小写字母跳过 
34                 continue; 35             else if(str[i]!=' ')//只剩下分割符号,除去空格 
36  { 37                 str.insert(i," ");//插入空格实现独立
38                 i++; 39  } 40              if(str[i]=='?')//'?'变 
41                 str[i]='!'; 42  } 43  v.clear(); 44         string temp=""; 45         //以空格为分隔将字符串拆分读入string数组 
46         for(int i=0;i<str.size();i++) 47  { 48             if(str[i]!=' ') 49                 temp+=str[i]; 50             else if(str[i]==' ') 51  { 52                 if(temp!="") 53  { 54  v.push_back(temp); 55                     temp=""; 56  } 57  } 58  } 59         //处理剩余字符串 
60         if(temp!="") 61  { 62  v.push_back(temp); 63             temp=""; 64  } 65         for(int i=0;i<v.size();i++) 66  { 67             if(v[i]=="I"||v[i]=="me") 68                 v[i]="you"; 69             else if(v[i]=="you") 70  { 71                 if(i<1||v[i-1].size()==1) 72                     continue; 73                 if(v[i-1]=="can"||v[i-1]=="could") 74  { 75                     v[i]=v[i-1];// 小细节 "can" or "could"
76                     v[i-1]="I"; 77  } 78  } 79  } 80         for(int i=0;i<v.size();i++) 81  { 82             cout<<v[i]; 83             if(i==v.size()-1)//防止越界导致段错误 
84                 continue; 85             if((v[i+1][0]>='a'&&v[i+1][0]<='z')||(v[i+1][0]>='0'&&v[i+1][0]<='9')||(v[i+1][0]=='I'))//后面字符串不为分隔符号
86                 cout<<" "; 87  } 88         cout<<endl; 89  } 90     return 0; 91 }

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM