天梯赛题目集 L1-064 估值一亿的AI核心代码 (20 分)
思路写前边:先把首尾空格收缩,用head,tail记录头和尾巴。
然后遍历s,将 ’I‘ 除外的大写转小写,并且把空格收缩(多个连续空格以及符号前面的空格,对于符号,我的判断是 不属于0-9 && 不属于a-z &&不属于 A-Z,因为这时候的字符串还有大写的字母,所以需要判断A-Z,后面的字符串转换特判其实可以不需要判断大写的A-Z了,但是cv 懒得改了= =)
然后进行字符串特判 以及边界特判,符合条件的跳跃下标到特判字符尾后输出(因为for循环里面最后还会i++,所以到特判字符尾再++就到了下一个字符),不符合就原样输出。
用if-else属实让我狠狠地难受了一把。刚开始写的时候没有构思好,后面越判断情况越复杂了,特别是对于独立性的判断,要判断越界情况(左为边界/右为边界/左右都是边界/左右都不是边界)。导致代码又臭又长,等有空学一下正则,用正则表达式写一个应该能轻松很多。
测试点2开始过不了,后来发现是把特判的s2[i+5] 写成了s[i+5]!!!
//AC代码 #include<bits/stdc++.h> using namespace std; int n; string s,s2=""; int main() { cin>>n; getchar(); while(n--) { s="",s2=""; getline(cin,s); cout<<s<<endl<<"AI: "; int head = 0,tail = s.size()-1,tail2 = 0; while(s[head] == ' ') head++; while(s[tail] == ' ') tail--; for(int i=head; i<=tail; i++) { if(s[i] <= 'Z' && s[i] >= 'A' && s[i] != 'I') { s2 += (s[i]+32); tail2++; } else if(s[i] == ' ') { if(i+1 <= tail && s[i+1] == ' ') { int cur = i+1; while(cur <= tail && s[cur] == ' ') cur++; i = cur; if((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= 'A' && s[i] <= 'Z') || (s[i] >= '0' && s[i] <= '9')) { s2 += ' '; tail2++; } i--; } else if((s[i+1] >= 'a' && s[i+1] <= 'z') || (s[i+1] >= 'A' && s[i+1] <= 'Z') || (s[i+1] >= '0' && s[i+1] <= '9')) { s2 += s[i]; tail2++; } } else { s2 += s[i]; tail2++; } } for(int i=0; i<tail2; i++) { if(s2[i] == '?') cout<<'!'; else if(s2[i] == 'c') {//还差这两个的独立性判断 if(i+8 < tail2) { if(i+9 < tail2 && i-1 >= 0) { if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z')) && !((s2[i+9] >= '0' && s2[i+9] <= '9') || (s2[i+9] >= 'A' && s2[i+9] <= 'Z') || (s2[i+9] >= 'a' && s2[i+9] <= 'z')) && s2[i+1] == 'o' && s2[i+2] == 'u' && s2[i+3] == 'l' && s2[i+4] == 'd' && s2[i+5] == ' ' && s2[i+6] == 'y' && s2[i+7] == 'o' && s2[i+8] == 'u') { cout<<"I could"; i += 8; } else // if(i+6 < tail2) { //can you if(i+7 < tail2 && i-1 >= 0) { //两端有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && !((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i+7 < tail2) { //右有空格 if(!((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i-1 >= 0) { //左有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else { //两端无空格 if( s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } } else cout<<s2[i]; // } else if(i+9 < tail2) { if(!((s2[i+9] >= '0' && s2[i+9] <= '9') || (s2[i+9] >= 'A' && s2[i+9] <= 'Z') || (s2[i+9] >= 'a' && s2[i+9] <= 'z')) && s2[i+1] == 'o' && s2[i+2] == 'u' && s2[i+3] == 'l' && s2[i+4] == 'd' && s2[i+5] == ' ' && s2[i+6] == 'y' && s2[i+7] == 'o' && s2[i+8] == 'u') { cout<<"I could"; i += 8; } else // if(i+6 < tail2) { //can you if(i+7 < tail2 && i-1 >= 0) { //两端有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && !((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i+7 < tail2) { //右有空格 if(!((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i-1 >= 0) { //左有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else { //两端无空格 if( s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } } else cout<<s2[i]; // } else if(i-1 >= 0) { if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z')) && s2[i+1] == 'o' && s2[i+2] == 'u' && s2[i+3] == 'l' && s2[i+4] == 'd' && s2[i+5] == ' ' && s2[i+6] == 'y' && s2[i+7] == 'o' && s2[i+8] == 'u') { cout<<"I could"; i += 8; } else // if(i+6 < tail2) { //can you if(i+7 < tail2 && i-1 >= 0) { //两端有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && !((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i+7 < tail2) { //右有空格 if(!((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i-1 >= 0) { //左有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else { //两端无空格 if( s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } } else cout<<s2[i]; // } else { if(s2[i+1] == 'o' && s2[i+2] == 'u' && s2[i+3] == 'l' && s2[i+4] == 'd' && s[i+5] == ' ' && s2[i+6] == 'y' && s2[i+7] == 'o' && s2[i+8] == 'u') { cout<<"I could"; i += 8; } else // if(i+6 < tail2) { //can you if(i+7 < tail2 && i-1 >= 0) { //两端有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && !((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i+7 < tail2) { //右有空格 if(!((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i-1 >= 0) { //左有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else { //两端无空格 if( s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } } else cout<<s2[i]; // } } else if(i+6 < tail2) { //can you if(i+7 < tail2 && i-1 >= 0) { //两端有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && !((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i+7 < tail2) { //右有空格 if(!((s2[i+7] >= '0' && s2[i+7] <= '9') || (s2[i+7] >= 'A' && s2[i+7] <= 'Z') || (s2[i+7] >= 'a' && s2[i+7] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else if(i-1 >= 0) { //左有空格 if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } else { //两端无空格 if( s2[i+1] == 'a' && s2[i+2] == 'n' && s2[i+3] == ' ' && s2[i+4] == 'y' && s2[i+5] == 'o' && s2[i+6] == 'u') { cout<<"I can"; i += 6; } else cout<<s2[i]; } } else cout<<s2[i]; } else if(s2[i] == 'I') { if(i == 0) { if(!((s2[i+1] >= '0' && s2[i+1] <= '9') || (s2[i+1] >= 'A' && s2[i+1] <= 'Z') || (s2[i+1] >= 'a' && s2[i+1] <= 'z') )) cout<<"you"; else cout<<s2[i]; } else if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && !((s2[i+1] >= '0' && s2[i+1] <= '9') || (s2[i+1] >= 'A' && s2[i+1] <= 'Z') || (s2[i+1] >= 'a' && s2[i+1] <= 'z') )) cout<<"you"; else cout<<s2[i]; } else if(s2[i] == 'm' && i+1 < tail2 && s2[i+1] == 'e') { if(i == 0) { if(!((s2[i+2] >= '0' && s2[i+2] <= '9') || (s2[i+2] >= 'A' && s2[i+2] <= 'Z') || (s2[i+2] >= 'a' && s2[i+2] <= 'z') )) { cout<<"you"; i++; } else cout<<s2[i]; } else if(!((s2[i-1] >= '0' && s2[i-1] <= '9') || (s2[i-1] >= 'A' && s2[i-1] <= 'Z') || (s2[i-1] >= 'a' && s2[i-1] <= 'z') ) && !((s2[i+2] >= '0' && s2[i+2] <= '9') || (s2[i+2] >= 'A' && s2[i+2] <= 'Z') || (s2[i+2] >= 'a' && s2[i+2] <= 'z') )) { cout<<"you"; i++; } else cout<<s2[i]; } else cout<<s2[i]; } cout<<endl; } return 0; }