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
    

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

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

  • 相关阅读:
    微信小程序之页面路由(九)
    Laravel生成Word文档
    ubuntu配置虚拟主机
    在eclipse中加入API文档帮助
    蓝桥杯java 基础练习 芯片测试
    Linux解压缩文件
    数据库范式(转)
    蓝桥杯java 基础练习 龟兔赛跑预测
    蓝桥杯java 算法提高 邮票面值设计
    蓝桥杯java 算法提高 统计单词数
  • 原文地址:https://www.cnblogs.com/Damitu/p/7655985.html
Copyright © 2011-2022 走看看