子串(牛客)

kmp匹配

暴力拆分,然后匹配就行

#include
using namespace std;
const int maxn = 1e6 + 5;
char s[maxn], s1[maxn];
int pos[maxn], flag;
stack st;
void pre(int len)
{
    int j = 0; pos[1] = 0;
    for(int i = 1; i < len; i++){
        while(j && s[j+1] != s[i+1]) j = pos[j];
        if(s[j+1] == s[i+1]) j++;
        pos[i+1] = j;
    }
}
void kmp(int len,int m)
{
    int j = 0;
    for(int i = 0; i < len; i++){
        while(j && s1[i+1] != s[j+1]) j = pos[j];
        if(s1[i+1] == s[j+1]) j++;
        if(j == m){ flag = 1; return ; }
    }
}
int main()
{
    int n, len, m = 0, x, p;
    scanf("%d%s",&n,s+1);
    len = strlen(s+1);
    for(int i = 1; i <= len; i++){
        if(s[i] >= '0' && s[i] <= '9') m = max(m,s[i]-'0');
        else m = max(m,s[i]-'A'+10);
    }
    pre(len);
    for(int i = m+1; i <= 16; i++){
        flag = 0; p = 0;
        for(int j = 1; j <= n; j++){
            x = j;
            while(x) st.push(x%i), x /= i;
            while(!st.empty()){
                int y = st.top(); st.pop();
                if(y >= 0 && y <= 9) s1[++p] = y+'0';
                else s1[++p] = y-10+'A';
            }
        }
        kmp(p,len);
        if(flag) break;
    }
    if(flag) printf("yes\n");
    else printf("no\n");
    return 0;
}

 

你可能感兴趣的:(子串(牛客))