Luogu2375 [NOI2014]动物园 (KMP)

写炸,上网,不同KMP形态。

无力,照该,一换写法就过。

横批:我是垃圾
\(next\)\(DP\)\(num\),路径压缩防卡\(n^2\)

AC

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 1000007;
const int mod = 1000000007;

int nxt[N], num[N];
char str[N];
int main() {
//FileOpen();

    int n;
    io >> n;
    while(n--) {
        scanf("%s", str);
        int len = strlen(str);
        int ans = 1;
        R(i,1,len) nxt[i] = 0;
        nxt[0] = -1;
        num[0] = 0, num[1] = 1;
        int i = 0, j = -1;
        while(i < len){
        	if(j == -1 || str[i] == str[j]){
        		nxt[++i] = ++j;
        		num[i] = num[j] + 1;
        	}
        	else
        		j = nxt[j];
        }
        i = 0, j = -1;
        while(i < len){
        	if(j == -1 || str[i] == str[j]){
        		++i, ++j;
        		while((j << 1) > i + 1) j = nxt[j];
        		ans = ans * 1ll * (num[j] + 1) % mod;
        	}
        	else
        		j = nxt[j];
        }
        
        printf("%d\n", ans);
    }
    return 0;
}

\(WA\)的原始代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 1000007;
const int mod = 1000000007;

int nxt[N], num[N];
char str[N];
int main() {
//FileOpen();

    int n;
    io >> n;
    while(n--) {
        scanf("%s", str);
        int len = strlen(str);
        int ans = 1;
        R(i,1,len) nxt[i] = 0;
        nxt[0] = -1;
        num[0] = 0, num[1] = 1;
        int i = 0, j = -1;
        while(i < len){
        	if(j == -1 || str[i] == str[j]){
        		nxt[++i] = ++j;
        		num[i] = num[j] + 1;
        	}
        	else
        		j = nxt[j];
        }
        i = 0, j = -1;
        while(i < len){
        	if(j == -1 || str[i] == str[j]){
        		++i, ++j;
        		while((j << 1) > i + 1) j = nxt[j];
        		ans = ans * 1ll * (num[j] + 1) % mod;
        	}
        	else
        		j = nxt[j];
        }
        
        printf("%d\n", ans);
    }
    return 0;
}

posted @ 2019-07-24 19:48  邱涵的秘密基地  阅读(130)  评论(0编辑  收藏  举报