TPA L1-064/估值一亿的AI核心代码/模拟/字符串处理

题目:https://pintia.cn/problem-sets/994805046380707840/problems/1111914599412858885

类别:模拟题/字符串处理

思路:离线处理。

关键函数:isalnum(); s.erase(i,3); s.insert(i,"you"); s.substr(i,3);

字符串知识:

https://www.cnblogs.com/happykoukou/p/5427268.html《字符串分割》就学会了用substr取子串

http://c.biancheng.net/view/400.html《字符串处理完全攻略》其他函数也可以来这里找。

https://www.cnblogs.com/hailong230013/p/14068274.html《判断字符串是否包含某子串》a.find("you")  <string>

 

第一阶段:差一个点过不了。不知道哪里错了。

#include<bits/stdc++.h>
using namespace std;
string s;
int po,ta;

int search(){
    int i,flag=0;
    //cout<<"yes\n";
    for(i=ta-2;i+6<s.length();i++){
        //cout<<i<<" "<<s.length()<<s.substr(i,9)<<endl;
        if(s.substr(i,7)=="can you"&&(i==0||!isalnum(s[i-1]))&&(i+6==s.length()||!isalnum(s[i+7]))){
            po=i;ta=i+6;
            //cout<<"youle\n";
            return 1;
        }
    }
    return 0;
}
int search2(){
    int i,flag=0;
    //cout<<"yes\n";
    for(i=ta-2;i+8<s.length();i++){
        //cout<<i<<" "<<s.length()<<s.substr(i,9)<<endl;
        if(s.substr(i,9)=="could you"&&(i==0||!isalnum(s[i-1]))&&(i+8==s.length()||!isalnum(s[i+9]))){
            po=i;ta=i+8;
            //cout<<"youle\n";
            return 1;
        }
    }
    return 0;
}

int change(){
    s.erase(po,ta-po+1);
    s.insert(po,"I can");
}
int change2(){
    s.erase(po,ta-po+1);
    s.insert(po,"I could");
}
int main(){
    cin>>t;
    getchar();
    while(t--){
        getline(cin,s);
        cout<<s<<endl;
        while(s[0]==' '){
            s.erase(0,1);
        }
        while(s[s.length()]==' '){
            s=s.substr(0,s.length()-1);
        }
        //cout<<s<<endl;
        int l=0;
        while(l<s.length()){
            while(s[l]==' '&&s[l+1]==' '){
                s.erase(l+1,1);
            }
            if(s[l]==' '&&!isalnum(s[l+1]))
                s.erase(l,1);
            l++;
            
        }
        //cout<<s<<endl;
        int i;
        for(i=0;i<=s.length();i++){
            if(s[i]!='I'&&s[i]>64&&s[i]<91){
                s[i]+=32;
            }
        }
        ta=2;
        while(search()){
            change();
        }
        ta=2;
        
        while(search2()){
            change2();
        }
        
        //cout<<s<<endl;
        
        while(1){
            int flag=0;
            for(i=0;i<s.length();i++){
                if(s[i]=='I'&&(i==0||!isalnum(s[i-1]))&&(i==s.length()-1||!isalnum(s[i+1]))&&(i+5>s.length()||s.substr(i,5)!="I can")&&(i+7>s.length()||s.substr(i,7)!="I could")){
                    //cout<<i<<" "<<s[i]<<" "<<s<<" yes\n";
                    flag=1;break;
                }
            }
            if(!flag)break;
            //cout<<flag<<" "<<i<<endl;
            s.erase(i,1);
            s.insert(i,"you");            
        }
        
        while(1){
            int flag=0;
            for(i=0;i+1<s.length();i++){
                //if(i==2)cout<<s[i-1]<<" "<<s[i]<<" "<<s.length()-2<<" "<<
                if((i==0||!isalnum(s[i-1]))&&(i==s.length()-2||!isalnum(s[i+2]))&&s.substr(i,2)=="me"){
                    flag=1;
                    //cout<<i<<" "<<s.length()<<" "<<s[i]<<" "<<s<<" yes\n";
                    break;
                }
            }
            if(!flag)break;
            s.erase(i,2);
            s.insert(i,"you");            
        }
        
        for(i=0;i<s.length();i++){
            if(s[i]=='?'){
                s[i]='!';
            }
        }
        
        cout<<"AI: "<<s<<endl;
    }
}
View Code

 

第二阶段:暂无。

 

网上资料:《只用了48行的算法》

https://blog.csdn.net/qq_41829380/article/details/88919000

核心函数:stringstream  介绍:https://blog.csdn.net/liitdar/article/details/82598039《常见用法介绍》拼接字符串并输出?

 

对着这个代码抄,还是出故障了。

故障前:

#include<bits/stdc++.h>
using namespace std;
string s,la;
string str[1010];

int main(){
    int t;
    cin>>t;getchar();
    while(t--){
        getline(cin,s);
        cout<<s<<endl<<"AI:";
        for(int i=0;i<s.length();i++){
            if(isalnum(s[i])){
                if(s[i]!='I') 
                    //s[i]=s[i]+'a'-'A';
                    s[i]=tolower(s[i]);
            }
            else{
            //if(!isalnum(s[i])&&s[i]!=' ')
                s.insert(i," "),i++;
            }
            
            if(s[i]=='?')s[i]='!';
        }
        //memset(str,0,sizeof(str));
        stringstream ss(s);
        int nu=0;
        while(ss>>s){
            str[nu++]=s;
        }
        if(!isalnum(str[0][0]))cout<<" ";
        for(int i=0;i<nu;i++){
            if(!isalnum(str[i][0]))cout<<str[i];
            else
            if((str[i]=="can"||str[i]=="could")&&i+1<nu&&str[i+1]=="you"){
                cout<<" I "<<str[i];i++;
            }else
            if(str[i]=="I"||str[i]=="me")cout<<" you";
            else
            cout<<" "<<str[i];
            
        }
        cout<<endl;
        
        
        
        /*
        stringstream ss(s);
        int flag=1,bdj=0;
        while(ss>>s){
            if(flag&&!isalnum(s[0]))cout<<" ";
            flag=0;
            if(bdj&&s=="you"){
                cout<<" I "<<la;bdj=0;
            }else{
                if(bdj){
                    if(isalnum(la[0]))cout<<" "<<la;
                    else cout<<s;
                    bdj=0;
                }
                if(s=="can"||s=="could"){
                    bdj=1;la=s;
                }else if(s=="I"||s=="me"){
                    cout<<" you";
                }else if(!isalnum(s[0]))cout<<s;
                else cout<<" "<<s;
                
            }
        }
        cout<<endl; 
        */
        //cout<<s<<endl;
        
    }
}
View Code

重置数组str。

 故障后:

#include<bits/stdc++.h>
using namespace std;
string s,la;
string str[1010];

int main(){
    int t;
    cin>>t;getchar();
    while(t--){
        getline(cin,s);
        cout<<s<<endl<<"AI:";
        for(int i=0;i<s.length();i++){
            if(isalnum(s[i])){
                if(s[i]!='I') 
                    //s[i]=s[i]+'a'-'A';
                    s[i]=tolower(s[i]);
            }
            else{
            //if(!isalnum(s[i])&&s[i]!=' ')
                s.insert(i," "),i++;
            }
            
            if(s[i]=='?')s[i]='!';
        }
        //memset(str,0,sizeof(str));
        stringstream ss(s);
        int nu=0;
        while(ss>>s){
            str[nu++]=s;
        }
        if(!isalnum(str[0][0]))cout<<" ";
        for(int i=0;i<nu;i++){
            if(!isalnum(str[i][0]))cout<<str[i];
            else
            if((str[i]=="can"||str[i]=="could")&&i+1<nu&&str[i+1]=="you"){
                cout<<" I "<<str[i];i++;
            }else
            if(str[i]=="I"||str[i]=="me")cout<<" you";
            else
            cout<<" "<<str[i];
            
        }
        cout<<endl;
        
        
        for(int i=0;i<nu;i++)str[i]="";
        
        /*
        stringstream ss(s);
        int flag=1,bdj=0;
        while(ss>>s){
            if(flag&&!isalnum(s[0]))cout<<" ";
            flag=0;
            if(bdj&&s=="you"){
                cout<<" I "<<la;bdj=0;
            }else{
                if(bdj){
                    if(isalnum(la[0]))cout<<" "<<la;
                    else cout<<s;
                    bdj=0;
                }
                if(s=="can"||s=="could"){
                    bdj=1;la=s;
                }else if(s=="I"||s=="me"){
                    cout<<" you";
                }else if(!isalnum(s[0]))cout<<s;
                else cout<<" "<<s;
                
            }
        }
        cout<<endl; 
        */
        //cout<<s<<endl;
        
    }
}
View Code

 

但是不知道怎么从大模拟的角度优化,也不知道如果不用str数组怎么过。

 

posted @ 2021-03-08 19:54  windiest  阅读(52)  评论(0编辑  收藏  举报