zoukankan      html  css  js  c++  java
  • [POJ2406]Power Strings

    题目链接:http://poj.org/problem?id=2406

    Description

    Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

    Input

    Each test case is a line of input representing s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

    Output

    For each s you should print the largest n such that s = a^n for some string a.

    Sample Input

    abcd
    aaaa
    ababab
    .
    

    Sample Output

    1
    4
    3
    

    Hint

    This problem has huge input, use scanf instead of cin to avoid time limit exceed.

    Source

     
     
    给你一个字串,这个串是里面的某子串循环n次得到的,求这个n。
    KMP求循环节,两个公式:如果n % (n - pre[n]) == 0说明存在循环节,循环次数为n / (n - pre[n])
     
     
     
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <iostream>
     6 #include <cmath>
     7 #include <queue>
     8 #include <map>
     9 #include <stack>
    10 #include <list>
    11 #include <vector>
    12 
    13 using namespace std;
    14 
    15 const int maxn = 66666666;
    16 int na, nb;
    17 char a[maxn];
    18 char b[maxn];
    19 int pre[maxn];
    20 
    21 //b是模式串,a是目标串
    22 void getpre(char *b, int *pre) {
    23     int j, k;
    24     pre[0] = -1;
    25     j = 0;
    26     k = -1;
    27     while(j < nb) {
    28         if(k == -1 || b[j] == b[k]) {
    29             j++;
    30             k++;
    31             pre[j] = k;
    32         }
    33         else {
    34             k = pre[k];
    35         }
    36     }
    37 }
    38 
    39 int kmp() {
    40     int ans = 0;
    41     int i = 0;
    42     int j = 0;
    43     getpre(b, pre);
    44     while(i < na) {
    45         if(j == -1 || a[i] == b[j]) {
    46             i++;
    47             j++;
    48         }
    49         else {
    50             j = pre[j];
    51         }
    52         if(j == nb) {
    53             ans++;
    54         }
    55     }
    56     return ans;
    57 }
    58 
    59 int main() {
    60     freopen("in", "r", stdin);
    61     while(~scanf("%s", b) && strcmp(".", b)) {
    62         nb = strlen(b);
    63         getpre(b, pre);
    64         int loop = nb - pre[nb];
    65         if(nb % loop == 0) {
    66             printf("%d
    ", nb / loop);
    67         }
    68         else {
    69             printf("1
    ");
    70         }
    71     }
    72 }
  • 相关阅读:
    1-1 10:超级玛丽游戏
    1-1 09:字符菱形
    【Lucene4.8教程之四】分析
    【Lucene4.8教程之六】QueryParser与Query子类:如何生成Query对象
    【Lucene4.8教程之三】搜索
    Java路径问题最终解决方案—可定位所有资源的相对路径寻址
    java.util.logging.Logger基础教程
    【Lucene4.8教程之二】索引
    【Lucene4.8教程之一】使用Lucene4.8进行索引及搜索的基本操作
    重要学习参考资料
  • 原文地址:https://www.cnblogs.com/kirai/p/4761550.html
Copyright © 2011-2022 走看看