403 循环同构判断

// 403 循环同构判断.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

/*
http://oj.daimayuan.top/course/22/problem/936

给你两个字符串 a,b
,字符串均由小写字母组成,你需要判断这两个字符串是否循环同构。

是的话输出 Yes,否则输出 No。

输入格式
第一行一个字符串 a
。

第二行一个字符串 b
。

输出格式
输出一行一个字符串表示答案。

样例输入1
baabaac
cbaabaa
样例输出1
Yes
样例输入2
aaab
caaa
样例输出2
No
数据规模
对于所有数据,保证 1≤|a|,|b|≤105
,字符串均由小写字母构成。
*/

#include <iostream>
#include <cstring>


using namespace std;

char s[200002], t[200002];

string getmin(char s[]) {
	int n = strlen(s + 1);
	for (int l = 1; l <= n; l++)
		s[l + n] = s[l];
	int i = 1, j = 2;
	while (j <= n) {
		int k = 0;
		while (k < n && s[i + k] == s[j + k])
			++k;
		if (s[i + k] > s[j + k])
			i += k + 1;
		else
			j += k + 1;
		if (i == j)
			++j;
		if (i > j)
			swap(i, j);
	}
	string res = "";
	for (int l = i; l <= i + n - 1; l++)
		res += s[l];
	return res;
}

int main()
{
	cin >> s + 1 >> t + 1;
	if (getmin(s) == getmin(t))
		cout << "Yes" << endl;
	else
		cout << "No" << endl;

	return 0;
}

 

posted on 2024-12-25 16:55  itdef  阅读(9)  评论(0)    收藏  举报

导航