• 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
  • 相关阅读:
    Mac连接远程Linux管理文件(samba)
    centos6 防火墙iptables操作整理
    安装postgresql
    linux CentOS6.5 yum安装mysql 5.6
    centos--git搭建之Gogs安装
    查看mysql 默认端口号和修改端口号
    centos之mysql安装配置使用
    流媒体服务器SRS部署
    redis配置文件参数说明
    redis 安装
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3956794.html
Copyright © 2020-2023  润新知