zoukankan      html  css  js  c++  java
  • poj2406 Power Strings

    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.
     
    题意是给一个串s,求有没有一个串能通过复制自己变成s。求出最大的长度
    这题跟那个bzoj1355很像……
    最长的串的长度应该是n-next[n],然后判断n能不能整除n-next[n],如果能输出商,不能输出1
    #include<cstdio>
    #include<cstring>
    #define LL long long
    inline void write(LL a)
    {
    	if (a<0){printf("-");a=-a;}
    	if (a>=10)write(a/10);
    	putchar(a%10+'0');
    }
    inline void writeln(LL a){write(a);printf("
    ");}
    int next[1000010];
    char s[1000010];
    int l,j,k;
    inline void pre()
    {
    	memset(next,0,sizeof(next));
    	j=0;
    	for (int i=2;i<=l;i++)
    	{
    	  	while (j>0&&s[j+1]!=s[i])j=next[j];
    	  	if (s[j+1]==s[i])j++;
    	  	next[i]=j;
    	}
    }
    int main()
    {
    	while (scanf("%s",s+1)&&s[1]!='.')
    	{
    		l=strlen(s+1);
    		pre();
    		int len=l-next[l];
    		if (l%len)writeln(1);
    		else writeln(l/len);
    	}
    	return 0;
    }
    
    ——by zhber,转载请注明来源
  • 相关阅读:
    96. 不同的二叉搜索树
    95. 不同的二叉搜索树 II
    94. 二叉树的中序遍历
    93. 复原IP地址
    python-007(用户登录(三次机会重试))
    python-006求1-2+3-4+5.....99的所有数的和
    python006(求1-2+3-4+5.....99的所有数的和)
    python-005(1-100奇数和偶数)
    python-004(while循环)
    python-003(if...elif...else)
  • 原文地址:https://www.cnblogs.com/zhber/p/4162960.html
Copyright © 2011-2022 走看看