【BZOJ 1398】 Necklace
【题目链接】
https://www.lydsy.com/JudgeOnline/problem.php?id=1398
【算法】
最小表示法
【代码】
#include<bits/stdc++.h> using namespace std; #define MAXN 1000010 char s1[MAXN<<1],s2[MAXN<<1],a[MAXN],b[MAXN]; inline void get(char *t,char *a) { int i,j,k,n,pos; n = strlen(a+1); for (i = 1; i <= n; i++) a[i+n] = a[i]; i = 1; j = 2; while (i <= n && j <= n) { for (k = 0; k <= n && a[i+k] == a[j+k]; k++); if (k == n) break; if (a[i+k] > a[j+k]) { i = i + k + 1; if (i == j) i++; } else { j = j + k + 1; if (i == j) j++; } } pos = min(i,j); for (i = pos; i <= n; i++) t[i-pos] = a[i]; for (i = 1; i < pos; i++) t[n-pos+i] = a[i]; } int main() { scanf("%s%s",s1+1,s2+1); get(a,s1); get(b,s2); if (strcmp(a,b) == 0) printf("Yes\n%s\n",a); else printf("No\n"); return 0; }