zoukankan      html  css  js  c++  java
  • POJ 2406Power Strings(KMP)

    POJ 2406

    其实就是一个简单的kmp应用:

    ans = n % (n - f[n]) == 0 ? n / (n - f[n]) : 1

    其中f是失配函数

     1 //#pragma comment(linker, "/STACK:1677721600")
     2 #include <map>
     3 #include <set>
     4 #include <stack>
     5 #include <queue>
     6 #include <cmath>
     7 #include <ctime>
     8 #include <vector>
     9 #include <cstdio>
    10 #include <cctype>
    11 #include <cstring>
    12 #include <cstdlib>
    13 #include <iostream>
    14 #include <algorithm>
    15 using namespace std;
    16 #define INF 0x3f3f3f3f
    17 #define inf (-((LL)1<<40))
    18 #define lson k<<1, L, (L + R)>>1
    19 #define rson k<<1|1,  ((L + R)>>1) + 1, R
    20 #define mem0(a) memset(a,0,sizeof(a))
    21 #define mem1(a) memset(a,-1,sizeof(a))
    22 #define mem(a, b) memset(a, b, sizeof(a))
    23 #define FIN freopen("in.txt", "r", stdin)
    24 #define FOUT freopen("out.txt", "w", stdout)
    25 #define rep(i, a, b) for(int i = a; i <= b; i ++)
    26 #define dec(i, a, b) for(int i = a; i >= b; i --)
    27 
    28 template<class T> T MAX(T a, T b) { return a > b ? a : b; }
    29 template<class T> T MIN(T a, T b) { return a < b ? a : b; }
    30 template<class T> T GCD(T a, T b) { return b ? GCD(b, a%b) : a; }
    31 template<class T> T LCM(T a, T b) { return a / GCD(a,b) * b;    }
    32 
    33 //typedef __int64 LL;
    34 typedef long long LL;
    35 const int MAXN = 1000000 + 10;
    36 const int MAXM = 110000;
    37 const double eps = 1e-8;
    38 LL MOD = 1000000007;
    39 
    40 char s[MAXN];
    41 int f[MAXN];
    42 
    43 void get_next(int n) {
    44     mem0(f);
    45     rep (i, 1, n - 1) {
    46         int j = f[i];
    47         while(j && s[i] != s[j])  j = f[j];
    48         f[i + 1] = s[i] == s[j] ? j + 1 : 0;
    49     }
    50 }
    51 
    52 int main()
    53 {
    54     //FIN;
    55     while(~scanf("%s", s) && s[0] != '.') {
    56         int n = strlen(s);
    57         get_next(n);
    58         printf("%d
    ", n % (n - f[n]) == 0 ? n / (n - f[n]) : 1);
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    【题解】一本通例题 S-Nim
    【题解】一本通例题 取石子游戏
    【题解】[USACO09NOV]A Coin Game S
    【题解】取火柴游戏
    【题解】CF375D Tree and Queries
    Linux sudo用户提权与日志审计
    No space left on device
    CentOS下多种方法显示文本行号
    Python之行-01之初识python
    约瑟夫问题
  • 原文地址:https://www.cnblogs.com/gj-Acit/p/4678619.html
Copyright © 2011-2022 走看看