• POJ2406 Power Strings


    传送门

    Power Strings
    Time Limit: 3000MS   Memory Limit: 65536K
         

    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

    第一道kmp题。。题目意思是求一个字符串的循环节,求出循环节长度之后检验即可

     1 #include<set>
     2 #include<queue>
     3 #include<cstdio>
     4 #include<cstdlib>
     5 #include<cstring>
     6 #include<iostream>
     7 #include<algorithm>
     8 using namespace std;
     9 const int N = 1000010;
    10 #define For(i,n) for(int i=1;i<=n;i++)
    11 #define Rep(i,l,r) for(int i=l;i<=r;i++)
    12 char s[N];
    13 int next[N],n;
    14 
    15 void BuildNext(char s[]){
    16     next[0]=next[1]=0;
    17     For(i,n-1){
    18         int j=next[i];
    19         while(j&&s[i]!=s[j]) j=next[j];
    20         if(s[i]==s[j])  next[i+1]=j+1;
    21         else            next[i+1]=0;
    22     }
    23 }
    24 
    25 int main(){
    26     while(scanf("%s",&s),s[0]!='.'){
    27         n=strlen(s);BuildNext(s);
    28         int rpt = n-next[n];
    29         int i = n;
    30         while(i&&i-next[i]==rpt) i=next[i];
    31         if(i) printf("1
    ");
    32         else  printf("%d
    ",n/rpt);
    33     }
    34     return 0;
    35 }
    Codes
  • 相关阅读:
    可视化工具 kibana 的安装和使用
    常见的数据类型
    Elastic Search 分词器的介绍和使用
    基于 TCP 协议的网络编程
    Java7 的 NIO.2
    NIO(New IO)
    Java9 改进的对象序列化
    反射和泛型
    使用反射生成 JDK 动态代理
    使用反射生成并操作对象
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3956794.html
Copyright © 2020-2023  润新知