KMP POJ 2406 Power Strings

 

题目传送门

 1 /*
 2     题意:一个串有字串重复n次产生,求最大的n
 3     KMP:nex[]的性质应用,感觉对nex加深了理解
 4 */
 5 /************************************************
 6 * Author        :Running_Time
 7 * Created Time  :2015-8-10 10:51:54
 8 * File Name     :POJ_2406.cpp
 9  ************************************************/
10 
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29 
30 #define lson l, mid, rt << 1
31 #define rson mid + 1, r, rt << 1 | 1
32 typedef long long ll;
33 const int MAXN = 1e6 + 10;
34 const int INF = 0x3f3f3f3f;
35 const int MOD = 1e9 + 7;
36 char str[MAXN];
37 int nex[MAXN];
38 
39 int get_nex(void)   {
40     int len = strlen (str);
41     int i = 0, j = -1;  nex[0] = -1;
42     while (i < len) {
43         if (j == -1 || str[j] == str[i])    {
44             j++;    i++;    nex[i] = j;
45         }
46         else    j = nex[j];
47     }
48     if (i % (i - nex[i]) == 0)  {
49         return i / (i - nex[i]);
50     }
51     else    return 1;
52 }
53 
54 int main(void)    {     //POJ 2406 Power Strings
55     while (scanf ("%s", str) == 1)  {
56         if (str[0] == '.')  break;
57         printf ("%d\n", get_nex ());
58     }
59 
60     return 0;
61 }

 

转载于:https://www.cnblogs.com/Running-Time/p/4717790.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值