poj1936 All in ALL

//题意:给你两个串,问第二个串是否包含第一个串,不是连续字串,是公共串

//思路:直接暴力,从第一个字符向后判断,每次记录下标,然后从下一个字符开始继续便利,所以第二个串最多遍历一次,第一个串也最多遍历一次并不会超时非常简单,具体见代码:

//M: 928k T:0MS
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s1, s2;
    while(cin >> s1 >> s2){
        int len1 = s1.length();
        int len2 = s2.length();
        if(len1 == len2){
            if(s1 == s2)
                cout << "Yes" << endl;
            else
                cout << "No" << endl;
        }else if(len1 < len2){
            int st = 0;//记录母串下标也就是第二个数组
            int cnt = 0;
            for(int i = 0; i < len1; i++){
                for(int j = st; j < len2;){
                    if(s1[i] == s2[j]){
                        st = ++j;
                        cnt++;
                        break;
                    }else{
                        st = ++j;
                    }
                }
            }
            if(cnt == len1)
                cout << "Yes" << endl;
            else
                cout << "No" << endl;
        }else{
            cout << "No" << endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/small__snail__5/article/details/80161761
ALL