zoukankan      html  css  js  c++  java
  • 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("
    
    ----------
    
    ")
    #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
    ", 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("
    
    ----------
    
    ")
    #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
    ", ans);
        }
        return 0;
    }
    

  • 相关阅读:
    常用颜色代码 (30种)
    C++ 调用lib 和 dll的 方法 及 动态库DLL与静态库lib的区别
    如何: 通过HTML文档对象模型访问文档中的ActiveX控件的属性 .
    理解预编译头
    #ifndef用法总结 .
    C++中MessageBox()的详细用法
    如何用Javascript捕获ActiveX对象的事件
    ActiveX控件调用JavaScript函数的方法
    MFC Activex与JavaScript的接口交互
    include包含文件查找的顺序 .
  • 原文地址:https://www.cnblogs.com/bingoyes/p/11240421.html
Copyright © 2011-2022 走看看