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

传送门

题意:

 

思路:

· 个人思路是先处理原文串a所有空格和大小写变成串b

 · 再处理串b中的各种替换问题,由于在处理单词替换时虽然先考虑的“I”的替换,但是之后再不会变量到该单词,所以并未出现大家所说的测试点2(can you)的卡顿。

· 但是特别注意这个题的另外一个坑点,本人就是一直测试点1和测试点4格式错误,后面通过这篇博客的详细讲解才通过了本题。

· 测试点1:结尾没有标点符号而有多个空格

· 测试点4:三个空格,要求输出"AI: "即可。

AC代码:

#include<bits/stdc++.h>
#define int long long
//#define endl '\n'
using namespace std;
const int N = 1e5+10;

int n;
string a, b, c;

bool check(int x, int sz, int len)
{
    if((x==0||b[x-1]==' '||ispunct(b[x-1]))&&(x+(sz-1)==len||b[x+sz]==' '||ispunct(b[x+sz]))) return 1;
    return 0;
}

signed main()
{
    cin >> n;
    getchar();
    while(n--){
        a.clear(); b.clear(); c.clear();
        getline(cin, a);
        cout << a << endl;
        int len = a.size()-1;
        for(int i = 0; i <= len; i ++){
            if(a[i]==' '){
    //            cout << "i: " << i << endl;
                if(!b.size()||i==len) continue;
                int ok = 0;
                for(int j = i+1; j <= len; j ++){
                    if(a[j]==' ') continue;
                    else if(i==len){
                        i = j-1;
                        ok = 1;
                        break;
                    }
                    else if(ispunct(a[j])){
                        i = j;
                        ok = 1;
                        if(a[j]=='?') b += '!';
                        else b += a[j];
                        break;
                    }
                    else{
                        ok = 1;
                        if(b.size()) b += ' ';
                        i = j-1;
                        break;
                    }
                }
            }
            else if(isupper(a[i])&&a[i]!='I') b += tolower(a[i]);
            else if(a[i]=='?') b += '!';
            else b += a[i];
        }

//        cout << "size: " << b.size() << endl;
//        cout << b << endl;

        len = b.size()-1;
        for(int i = 0; i <= len; i ++){
//            cout << "i: " << i << endl;
            if(b[i]=='I'&&check(i, 1, len)){
                c += "you";
                continue;
            }
            if(b[i]=='m'&&b[i+1]=='e'&&check(i, 2, len)){
                c += "you";
                i++;
                continue;
            }
            if(i+6<=len){
                string tmp = b.substr(i, 7);
                if(tmp=="can you"&&check(i, 7, len)){
                    c += "I can";
                    i += 6;
                    continue;
                }
            }
            if(i+8<=len){
                string tmp = b.substr(i, 9);
                if(tmp=="could you"&&check(i, 9, len)){
                    c += "I could";
                    i += 8;
                    continue;
                }

            }
            c += b[i];
        }


//        cout << "size: " << c.size() << "c:" << c << "endl" << endl;
        cout << "AI: " << c << endl;
    }



    return 0;
}



/*

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

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值