zoukankan      html  css  js  c++  java
  • 【POJ 2406 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

    Waterloo local 2002.07.01

    题解:
          ①找出最小循环节————KMP

    #include<stdio.h>
    #include<cstring>
    #define go(i,a,b) for(int i=a;i<=b;i++)
    const int N=4000003;int m,f[N],j;char P[N]; 
    int main()
    {
    	f[0]=f[1]=0;
    	while(scanf("%s",P),m=strlen(P),P[0]!='.')
    	{
    		go(i,1,m-1){j=f[i];while(j&&P[i]!=P[j])j=f[j];f[i+1]=P[i]==P[j]?j+1:0;}
    		printf("%d
    ",m%(m-f[m])?1:m/(m-f[m]));
    	}
    	return 0;
    }//Paul_Guderian
    

    镜子里面,像看到人生终点,或许再过上几年

    你也有张虚伪的脸,难道我们,是为了这样,才来到这世上……—————《你曾是少年》

  • 相关阅读:
    【BZOJ1645】[Usaco2007 Open]City Horizon 城市地平线 离散化+线段树
    【BZOJ4196】[Noi2015]软件包管理器 树链剖分
    【BZOJ4698】Sdoi2008 Sandy的卡片 后缀数组+RMQ
    【BZOJ4278】[ONTAK2015]Tasowanie 后缀数组
    mysql中使用concat例子
    SAP basis 常用事物
    推和敲
    踏和走
    下一个该你啦
    长城:恐惧的纪念碑
  • 原文地址:https://www.cnblogs.com/Damitu/p/7655985.html
Copyright © 2011-2022 走看看