Gym 101028F-Good Words

来源:互联网 发布:ow网络初始化失败 编辑:程序博客网 时间:2024/05/22 03:32

题目描述:

Zaid has two words, a of length between 4 and 1000 andb of length 4 exactly. The word a is 'good' if it has a substring which is equal to b. However, a is 'almost good' if by inserting a single letter inside of it, it would become 'good'. For example, ifa = 'start' and b = 'tear':b is not found inside of a, so it is not 'good', but if we inserted the letter 'e' inside ofa, it will become 'good' ('steart'), so a is 'almost good' in this case. Your task is to determine whether the worda is 'good' or 'almost good' or neither.

Input

The input consists of several test cases. The first line of the input contains a single integerT, the number of the test cases. Each of the followingT lines represents a test case and contains two space separated stringsa and b, each of them consists of lower case English letters. It is guaranteed that the length ofa is between 4 and 1000, and the length ofb is exactly 4.

Output

For each test case, you should output one line: if a is 'good' print 'good', ifa is 'almost good' print 'almost good', otherwise print 'none'.

Sample Input

Input
4smart markstart tearabracadabra crabtestyourcode your
Output
almost goodalmost goodnonegood
代码实现:

#include<iostream>#include<cstring>using namespace std;int main(){    int t,la,f,i,j;    char a[1100],b[1100],c[4][1100];    cin>>t;    while(t--)    {        cin>>a>>b;        la=strlen(a);        for(i=1; i<4; i++)        {            c[0][i-1]=b[i];        }        c[0][3]=NULL;        c[1][0]=b[0];        c[1][1]=b[2];        c[1][2]=b[3];        c[1][3]=NULL;        c[2][0]=b[0];        c[2][1]=b[1];        c[2][2]=b[3];        c[2][3]=NULL;        c[3][0]=b[0];        c[3][1]=b[1];        c[3][2]=b[2];        c[3][3]=NULL;//        cout<<a<<endl<<b<<endl;//        for(i=0;i<4;i++)//        {//            cout<<c[i]<<endl;//        }        f=0;        for(i=0; i<la; i++)        {            if(a[i]==b[0]&&a[i+1]==b[1]&&a[i+2]==b[2]&&a[i+3]==b[3])            {                cout<<"good"<<endl;                f=1;                break;            }        }        if(f==0)        {            for(i=0; i<la; i++)            {                if(a[i]==c[2][0]&&a[i+1]==c[2][1]&&a[i+2]==c[2][2])                {                    cout<<"almost good"<<endl;                    f=1;                    break;                }            }        }        if(f==0)        {            for(i=0; i<la; i++)            {                if(a[i]==c[0][0]&&a[i+1]==c[0][1]&&a[i+2]==c[0][2])                {                    cout<<"almost good"<<endl;                    f=1;                    break;                }            }        }        if(f==0)        {            for(i=0; i<la; i++)            {                if(a[i]==c[1][0]&&a[i+1]==c[1][1]&&a[i+2]==c[1][2])                {                    cout<<"almost good"<<endl;                    f=1;                    break;                }            }        }        if(f==0)        {            for(i=0; i<la; i++)            {                if(a[i]==c[2][0]&&a[i+1]==c[2][1]&&a[i+2]==c[2][2])                {                    cout<<"almost good"<<endl;                    f=1;                    break;                }            }        }        if(f==0)        {            for(i=0; i<la; i++)            {                if(a[i]==c[3][0]&&a[i+1]==c[3][1]&&a[i+2]==c[3][2])                {                    cout<<"almost good"<<endl;                    f=1;                    break;                }            }        }//        if(f==0)//        {//            for(i=0; i<la-3; i++)//            {//                for(j=0; j<4; j++)//                {//                    if(a[i]==c[j][0]&&a[i+1]==c[j][1]&&a[i+2]==c[j][2])//                    {//                        cout<<"almost good"<<endl;//                        f=1;//                        break;//                    }//                }//                if(f==1)//                {//                    break;//                }//            }//        }        if(f==0)        {            cout<<"none"<<endl;        }    }    return 0;}
错误代码:

#include<iostream>#include<cstring>using namespace std;int main(){    int t,la,f,i,j;    char a[1100],b[1100],c[4][1100];    cin>>t;    while(t--)    {        cin>>a>>b;        la=strlen(a);        for(i=1;i<4;i++)        {            c[0][i-1]=b[i];        }        c[0][3]=NULL;        c[1][0]=b[0];        c[1][1]=b[2];        c[1][2]=b[3];        c[1][3]=NULL;        c[2][0]=b[0];        c[2][1]=b[1];        c[2][2]=b[3];        c[2][3]=NULL;        c[3][0]=b[0];        c[3][1]=b[1];        c[3][2]=b[2];        c[3][3]=NULL;//        cout<<a<<endl<<b<<endl;//        for(i=0;i<4;i++)//        {//            cout<<c[i]<<endl;//        }        f=0;        for(i=0;i<la-3;i++)        {            //cout<<1;            if(a[i]==b[0]&&a[i+1]==b[1]&&a[i+2]==b[2]&&a[i+3]==b[3])            {                cout<<"good"<<endl;                f=1;                break;            }            for(j=0;j<4;j++)            {                if(a[i]==c[j][0]&&a[i+1]==c[j][1]&&a[i+2]==c[j][2])                {                    cout<<"almost good"<<endl;                    f=1;                    break;                }            }            if(f==1)            {                break;            }        }        if(f==0)        {            cout<<"none"<<endl;        }    }    return 0;}
如果a:mardsmark  b:mark

那么错误代码的输出结果:almost good,原因是从字符串的第一个字符开始遍历,如果遇到almost good的情况就会停止遍历,而不管后面时候会有good的情况

0 0