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,转载请注明来源
  • 相关阅读:
    2018年度总结
    SQL特殊comment语法
    mysql 主从复制change master to
    mysql 主从复制参数slave_net_timeout
    show master status, show slave status中的Executed_Gtid_Set
    mysql重复start slave、stop slave测试
    golang database sql DSN (Data Source Name)中的timeout, readTimeout
    mysql show master status为空值
    Linux 建立 TCP 连接的超时时间分析(解惑)
    golang 原子操作函数
  • 原文地址:https://www.cnblogs.com/zhber/p/4162960.html
Copyright © 2011-2022 走看看