1. 程式人生 > 其它 >天梯賽題目集 L1-064 估值一億的AI核心程式碼 (20 分)

天梯賽題目集 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; }