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

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

#include 

#define int ll
//#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
const int maxn=1e5+10;

void solve() {
    string str[1005];
    int cnt=0;
    string s;
    getline(cin,s);
    cout<<s<<"\n"<<"AI:";
    for (int i = 0; i < s.size(); ++i) {
        if (isupper(s[i])&&s[i]!='I') s[i]=tolower(s[i]);
        else if (!isalnum(s[i])) s.insert(i," "),i++;
        if (s[i]=='?') s[i]='!';
    }
    stringstream ss(s);
    while (ss>>s) str[cnt++]=s;
    if (!isalnum(str[0][0])) cout<<" ";
    for (int i = 0; i < cnt; ++i) {
        if (!isalnum(str[i][0])) cout<<str[i];
        else if ((str[i]=="can"||str[i]=="could")&&str[i+1]=="you")cout << " I " << str[i],i++;
        else if (str[i]=="I"||str[i]=="me") cout << " you";
        else cout << " " << str[i];
    }
    cout<<"\n";
}
signed main() {
    //ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;getchar();
    while (_--) {
        solve();
    }
    return 0;
}
#include
using namespace std;
int main() {
    int n;
    cin >> n;
    cin.get();
    while (n--) {
        string s;
        getline(cin, s);
        cout << s << endl;
        for (char & c : s) {
            if (c >='A' && c <= 'Z' && c != 'I') {
                c = c - 'A' + 'a';
            }
        }
        s = regex_replace(s, regex(" +"), " ");//消除多余空格
        s = regex_replace(s, regex("^ | $"), "");//消除首尾空格
        s = regex_replace(s, regex(" (\\W)"), "$1");//消除标点符号前的空格
        s = regex_replace(s, regex("\\?"), "!");//将?替换成!
 
        s = regex_replace(s, regex("\\bcan you\\b"), "A");//转换can you
        s = regex_replace(s, regex("\\bcould you\\b"), "B");//转换could you
        s = regex_replace(s, regex("\\b(I|me)\\b"), "C");//转换I、me
 
        s = regex_replace(s, regex("A"), "I can");
        s = regex_replace(s, regex("B"), "I could");
        s = regex_replace(s, regex("C"), "you");
        cout << "AI: " << s << endl;
    }
    return 0;
}

你可能感兴趣的:(模拟,pta,pta,c++)