Good Words Gym-101028F

Good Words Gym-101028F

标签:水题ios


题目连接c++

/* 题意:字符串a, b(长度固定为4)。 (1)若是b是a的子串,输出good; (2)不然,若在a中插入一个字符,b是a的子串的话,输出almost good; (3)两者都不行,输出none。 思路:C++函数a.substr(i,4)得到从i开始的长度为4的字串。 almost good: 转化为,枚举出b的四种可能串(去掉一个字符),若a中存在b的可能串,almost good。 */
#include <iostream>
#include <string>
using namespace std;

string a, b, b1, b2, b3, b4;

int main()
{
    int T;

    cin >> T;
    while(T--)
    {
        cin >> a >> b;
        int len = a.length();  //注意: for语句内runtime error,可能编译环境不一样
        int flag = 1;

        for(int i = 0; i < len - 3; i++)
            if(a.substr(i, 4) == b)
            {
                cout << "good" << endl;
                flag = 0;  break;
            }
        if(flag)
        {
            b1 = b.substr(0, 3);  //b的四个可能串
            b2 = b.substr(1, 3);
            b3 = b1;  b3[2] = b[3];
            b4 = b2;  b4[0] = b[0];
            //cout << b1 << " "<< b2 << " " << b3 << " " << b4 << endl;

            for(int i = 0; i < len - 2; i++)
                if(a.substr(i, 3) == b1 || a.substr(i, 3) == b2 || a.substr(i, 3) == b3 || a.substr(i, 3) == b4)
                {
                    cout << "almost good" << endl;
                    flag = 0;  break;
                }
        }
        if(flag)  cout << "none" << endl;
    }

    return 0;
}