Team ladder match L1-064 valuation of one hundred million of AI core code (string processing)

PTA L1-064 valuation of one hundred million of AI core code

There pit! Many

Topic Link

Title and analysis

Title:
This question asks you to achieve a little bit more worth of questions and answers in English AI program, the rules are:
      1. No matter what the user says, first of all they are saying as they are printed on one line;
      2. the elimination of excess space in the original: the neighbor a plurality of spaces into one space between words, the beginning and end of the row delete all spaces, the spaces in front of the punctuation deleted;
      3. the text in all capital letters to lower case, in addition to the I;
      4. the original All unique can you, could you replace the correspondence I can, I could-- where "independent" means that the word is separated spaces or punctuation;
      5. All the description and me into independent I you;
      ? 6. the original question marks into exclamation points in all;!
      7 in a row sentence after replacing output answers as AI

Direct ideas (there are BUG)

First, in accordance with the rules of one to one to come up with solutions:

  1. First direct output original words ( nonsense )
  2. Then delete the extra space
          2.1 to delete the beginning of the line trailing spaces, with a while has been to determine whether a space on the line
          between 2.2 word and a space in front of the sign, traversing the string, after finding a space, if the next is also a space to delete the current space; if The next word or punctuation, do not delete
  3. Traversal string, uppercase to lowercase letters, 'I' is not processed
  4. Or can you find a space after punctuation, could you, then replaced with I can, I could ( pit struck, the following will explain the pit )
  5. Basic with 4 spaces or punctuation I, me to replace you
  6. Traversal strings will? Replace!
  7. Output processing results

Wave analysis logic
execution order be 1-> 2.1> 2.2,3,6-> 4,5> 7

Pit

  1. The execution order of: processing space and can not be replaced in case a loop! ! ! Otherwise, the case is not good replacement space +
  2. When the replacement string, the replacement must be independent of string! ! ! As can you front and back must be a space or a string, but there is a special case! can you at the end of the string , so it is necessary to have to deal with this
  3. Special circumstances: when there is a lot of space in two punctuation marks , according to the rules, the output when the middle is leaving no space! Should pay attention to the output! ! ! Determining if the current bit is a space, and the next symbol is not output from this space.

AC Code

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
//@date: 2020-02-01 11:58:40
//@state:YES

int judge(char c)
{//判断传入的字符是什么
    if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
        return 1;//字母
    else if(c>='0'&&c<='9')
        return 2;//数字
    else if(c==' ')
        return 0;//空格
    else if(c>=33&&c<=126)
        return -1;//标点符号
    return 99;//默认值
}

string AI(string ask)
{//返回处理结果
    //删除行首空格
    while(ask[0]==' ')
        ask.erase(ask.begin());
    //删除行尾空格
    while(ask[ask.size()-1]==' ')
        ask.erase(ask.end()-1);
    
    for(int i=0;i<ask.size();i++)
    {
        //大写变小写
        if(ask[i]>='A'&&ask[i]<='Z'&&ask[i]!='I')
            ask[i]+=32;
        //?->!
        if(ask[i]=='?')
            ask[i]='!';
        //删除多余空格
        if(ask[i]==' ')
        {
            int j=i+1;
            while(ask[j]==' ') j++;
            ask.erase(ask.begin()+i+1,ask.begin()+j);//此处参数必须为迭代器,int不行
        }
    }
    //替换字符串
    for(int i=0;i<ask.size();i++)
    {
        if(i==0)
        {
            if(ask[i]=='I'&&(judge(ask[i+1])<=0||i+1==ask.size()))
                ask.replace(i,1,"you");
            else if(ask.substr(i,2)=="me"&&(judge(ask[i+2])<=0||i+2==ask.size()))
                ask.replace(i,2,"you");
            else if(ask.substr(i,7)=="can you"&&(judge(ask[i+7])<=0||i+7==ask.size()))
                ask.replace(i,7,"I can");
            else if(ask.substr(i,9)=="could you"&&(judge(ask[i+9])<=0||i+9==ask.size()))
                ask.replace(i,9,"I could");
        }
        if(judge(ask[i])<=0)
        {
            if(judge(ask[i+1])==1)//字母
            {
                if(ask[i+1]=='I'&&(judge(ask[i+2])<=0||i+2==ask.size()))
                    ask.replace(i+1,1,"you");
                if(ask.substr(i+1,2)=="me"&&(judge(ask[i+3])<=0||i+3==ask.size()))
                    ask.replace(i+1,2,"you");
                if(ask.substr(i+1,7)=="can you"&&(judge(ask[i+8])<=0||i+8==ask.size()))
                    ask.replace(i+1,7,"I can");
                if(ask.substr(i+1,9)=="could you"&&(judge(ask[i+10])<=0||i+10==ask.size()))
                    ask.replace(i+1,9,"I could");
            }
        }
    }

    return ask;
}

int main()
{
    int n;
    string ask,ans;
    cin>>n;
    cin.get();
    while(n--)
    {
        getline(cin,ask);
        cout<<ask<<endl;
        ans=AI(ask);
        cout<<"AI: ";
        for(int i=0;i<ans.size();i++)
        {
            //判断符号+空格+符号的特殊情况
            if(ans[i]==' '&&judge(ans[i+1])<=0)
                continue;
            else
                cout<<ans[i];
        }
        cout<<endl;
    }
    return 0;
}
Published 67 original articles · won praise 42 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_26235879/article/details/105357768