多校第八场——1009 Isomorphic Strings

Problem Description
It is preferrable to read the pdf statment.

Two strings are called cyclical isomorphic if one can rotate one string to get another one. ‘Rotate’ here means ‘‘to take some consecutive chars (maybe none) from the beginning of a string and put them back at the end of the string in the same order’’. For example, string ‘‘abcde’’ can be rotated to string ‘‘deabc’’.

Now that you know what cyclical isomorphic is, Cuber QQ wants to give you a little test.

Here is a string s of length n. Please check if s is a concatenation of k strings, s1,s2,⋯,sk (k>1), where,

k is a divisor of n;

s1,s2,…,sk are of equal length: nk;

There exists a string t, which is cyclical isomorphic with si for all 1≤i≤k.

Print ‘‘Yes’’ if the check is positive, or ‘‘No’’ otherwise.

Input
The first line contains an integer T (1≤T≤1000), denoting the number of test cases. T cases follow.

The first line of each test case contains an integer n (1≤n≤5⋅106).

The second line contains a string s of length n consists of lowercase letters only.

It is guaranteed that the sum of n does not exceed 2⋅107.

Output
For each test case, output one line containing ‘‘Yes’’ or ‘‘No’’ (without quotes).

Sample Input
6
1
a
2
aa
3
aab
4
abba
6
abcbcc
8
aaaaaaaa

Sample Output
No
Yes
No
Yes
No
Yes

题意:
给一个字符串 s ,长度为 n ,问是否存在一个 k ,满足 k∣n ,将 s 分成相等的 k 段子串,每一段子串互为循环同构。
思路:
在这里插入图片描述

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL mod = 13331;
const LL N = 5e6 + 10;
LL num[30];
LL ha[N];
LL p[N];
char s[N];
map<LL, LL> mp;
int main()
{
	p[0] = 1;
	for (LL i = 1; i < N; i++)
	{
		p[i] = p[i - 1] * mod;
	}
	LL T;
	scanf("%lld", &T);
	while (T--)
	{
		LL n;
		scanf("%lld", &n);
		scanf("%s", s + 1);
		memset(num, 0, sizeof(num));
		ha[0] = 0;
		for (LL i = 1; i <= n; i++)
		{
			num[s[i] - 'a' + 1]++;
			ha[i] = ha[i - 1] * mod + (s[i] - 'a' + 1);
		}
		LL gg = n;
		for (LL i = 1; i <= 26; i++)
		{
			if (num[i])
				gg = __gcd(gg, num[i]);
		}
		LL u = n / gg;
		LL flog = 1;
		if (u == 1 && n != 1)
		{
			flog = 0;
		}
		else
		{
			for (LL i = 1; i < gg; i++)
			{
				LL len = i * u;
				if (n%len)
				{
					continue;
				}
				flog = 0;
				mp.clear();
				LL k = ha[len] - ha[0] * p[len];
				mp[k] = 1;
				for (LL j = 1; j < len; j++)
				{
					k = k * mod + (s[j] - 96);
					mp[k - ha[j] * p[len]] = 1;
				}
				for (LL j = 1; j * len <= n; j++)
				{

					if (mp[ha[j * len] - ha[(j - 1) * len] * p[len]] == 0)
					{
						flog = 1;
						break;
					}
				}
				if (flog == 0)
				{

					break;
				}
			}
		}
		if (flog == 0)
		{
			cout << "Yes" << endl;
		}
		else
		{
			cout << "No" << endl;
		}
	}
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>