158. 项链 - AcWing题库
给定两个字符串,看看这两个字符串的最小表示是否一样
#include <bits/stdc++.h>
#include <iostream>
#include<unordered_map>
#include<unordered_set>
#define x first
#define y second
#define ios ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
typedef pair<int,int> PII ;
typedef pair<LL,int> PLI ;
const int N = 2e6+ 10 ;
const LL INF2 = 1e17;
int n,m;
char a[N],b[N];
int get(char s[])
{
int i=0,j=1;
while(i < n && j < n)
{
int k=0;
while(k < n && s[i + k ] == s[j + k ]) k ++ ;
if(k == n ) break;
if(s[i + k ] > s[j + k ]) i +=k +1;
else j += k + 1;
if(i == j ) i ++;
}
int res = min(i,j);s[res + n ] =0;
return res;
}
inline void solve()
{
cin >> a >> b;
n = ::strlen(a);
::memcpy(a+n,a,n);
::memcpy(b+n,b,n);
int ia = get(a),ib= get(b);
if(::strcmp(a+ia,b + ib) )
{
puts("No");
}
else
{
puts("Yes");puts(a+ia);
}
}
signed main()
{
ios
int T=1;
// cin>>T;
while(T -- )
{
solve();
}
return 0;
}