zoukankan      html  css  js  c++  java
  • poj1961Period【kmp next数组】

    大意:跟poj2406一样的题  思路见http://www.cnblogs.com/zhanzhao/p/4761477.html

    代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 using namespace std;
     5 
     6 const int maxn = 1000005;
     7 
     8 int next[maxn];
     9 
    10 void get(char *s) {
    11     int l = strlen(s);
    12     int j = 0, k = -1;
    13     next[0] = -1;
    14     while(j < l) {
    15         if(k == -1 || s[j] == s[k]) {
    16             
    17 
    18 
    19 //            if(s[++j] == s[++k]) {
    20 //                next[j] = next[k];
    21 //            } else {
    22 //                next[j] = k;
    23 //            }
    24             next[++j] = ++k;
    25         } else {
    26             k = next[k];
    27         }
    28     }
    29 }
    30 char s[maxn];
    31 
    32 int main() {
    33     int n;
    34     int kase = 1;
    35     while(scanf("%d",&n) && n) {
    36         scanf("%s",s);
    37         printf("Test case #%d
    ", kase++);
    38         get(s);
    39         int l = strlen(s);
    40 //        for(int i = 0; i <= l; i++) {
    41 //            printf("%d ", next[i]);
    42 //        }puts("");
    43         for(int i = 2; i <= l; i++) {
    44             int ans = 1;
    45             if(i % (i - next[i]) == 0) {
    46                 ans = i / (i - next[i]);
    47             }
    48             if(ans > 1) {
    49                 printf("%d %d
    ", i, ans);
    50             }
    51         }
    52         puts("");
    53     }
    54 }
    View Code
  • 相关阅读:
    hdu1915
    2014年9月28日 18:35:01
    洛谷—— P1122 最大子树和
    洛谷——P1103 书本整理
    洛谷—— P2049 魔术棋子
    UVA_1575
    洛谷—— P2424 约数和
    中文乱码问题
    JSP标签
    include指令
  • 原文地址:https://www.cnblogs.com/zhanzhao/p/4761549.html
Copyright © 2011-2022 走看看