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.

 

 

使用哈希的方法,把每个长度的字符串转化为数字存起来。

枚举将长串转换为短串的长度。

其中某一串的长度可以由  该公式求出:hashmap[i]=hashmap[i-1]*p[i-1];

由于我们是从短串到长串枚举,只要找到合适的字串就一定是最优解,直接break退出即可。

还有各个学校的oj不支持bits/stdc++.h,不支持强制转换成结构体类型,在写这些oj是尽量避免使用。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#define ll long long
#define ull unsigned long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--) 
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int maxl=1000005,base=233;
ull hashmap[maxl],p[maxl]={1};int len;
bool check(ull checknum,int k)
{
	for(int i=1;i<=len;i+=k)
	{
		//cout<<checknum<<' '<<(hashmap[i+k-1]-hashmap[i-1]*p[k])<<endl;
		if(checknum!=(hashmap[i+k-1]-hashmap[i-1]*p[k]))
		return 0;
	}
	return 1;
}
int main()
{
	ios
	p[0]=1;
	for(int i=1;i<=maxl-10;i++)
	{
		p[i]=p[i-1]*base;
	}
	
	char s[maxl];
	while(cin>>s+1)
	{
		if(s[1]=='.')break;
		len=strlen(s+1);
		for(int i=1;i<=len;i++)
		{
			hashmap[i]=hashmap[i-1]*base+s[i];
		}
		
		/*for(int i=1;i<=len;i++)
		{
			cout<<hashmap[i]<<' ';
		}
		cout<<endl;*/
		
		for(int i=1;i<=len;i++)
		{
			if(len%i)continue;
			ull checknum=hashmap[i];
			if(check(checknum,i))
			{
				cout<<len/i<<endl;
				break;
			}
		}
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值