AcWing 158. 项链
原题链接
简单
作者:
wjie
,
2020-08-01 13:07:55
,
所有人可见
,
阅读 554
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;
int n;
int getMin(string& str)
{
int i = 0, j = 1;
while (i < n && j < n)
{
int k = 0;
while (k < n && str[i+k] == str[j+k]) k++;
if (k == n) return i;
if (str[i+k] > str[j+k]) i += k + 1;
else j += k + 1;
if (i == j) i++;
}
return min(i, j);
}
int main()
{
string a, b;
cin >> a >> b;
n = a.size();
a += a;
int findSame = a.find(b);
if (findSame >= 0 && findSame < 2*n)
{
puts("Yes");
int head = getMin(a);
for (int i = 0; i < n; ++i) cout << a[head+i];
}
else puts("No");
return 0;
}