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

猛男就该写猛题

应该是过的代码中很短的一个了
里面也有很多小心思 算是生涯中比较优秀的代码吧
题目链接

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<bits/stdc++.h>
#define rep(i,s,e) for(int i=s; i<e; ++i)
using namespace std;
int main(){
int _; cin>>_; getchar(); while(_--){
string s; getline(cin,s); cout<<s<<"\nAI:";
while(s[0]==' ') s.erase(0,1); int len=s.length();
s=' '+s+' '; rep(i,1,len+1){
if(s[i]!='I'&&isupper(s[i])) s[i]=tolower(s[i]);
else if(!isalnum(s[i])&&s[i]!=' ') s.insert(i," "),s.insert(i+2,"*"),len+=2,i+=2;
}
while(s.find("?")!=string::npos) s.replace(s.find("?"),1,"!");
while(s.find(" ")!=string::npos) s.replace(s.find(" "),2," ");
while(s.find(" I ")!=string::npos) s.replace(s.find(" I "),3," You ");
while(s.find("*I ")!=string::npos) s.replace(s.find("*I "),3,"*You ");
while(s.find(" me ")!=string::npos) s.replace(s.find(" me "),4," You ");
while(s.find("*me ")!=string::npos) s.replace(s.find("*me "),4,"*You ");
while(s.find(" can you ")!=string::npos) s.replace(s.find(" can you "),9," I can* ");
while(s.find("*can you ")!=string::npos) s.replace(s.find("*can you "),9,"*I can ");
while(s.find(" could you ")!=string::npos) s.replace(s.find(" could you "),11," I could ");
while(s.find("*could you ")!=string::npos) s.replace(s.find("*could you "),11,"*I could ");
len=s.length(); rep(i,0,len) if(s[i]=='Y') s[i]='y'; else if(s[i]=='*') s.erase(i,1),len--,i--;
rep(i,2,len) if(!isalnum(s[i])&&s[i]!=' '&&s[i-1]==' ') s.erase(i-1,1),len--,i--;
while(s[len-1]==' ') s.erase(len-1,1),len--;
if(!len) cout<<' '; cout<<s<<'\n';
}
}