POJ - 1509 Glass Beads
最小表示法模板
//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<ctime>
const int N=100007;
typedef long long LL;
using namespace std;
int T,n;
char s[N];
template<typename T> void read(T &x) {
T f=1; x=0; char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
if(ch=='-') f=-1,ch=getchar();
for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}
int solve() {
int i=0,j=1,k=0;
for(;i<n&&j<n&&k<n;) {
int t=s[(i+k)%n]-s[(j+k)%n];
if(!t) k++;
else {
if(t>0) i=i+k+1;
else j=j+k+1;
if(i==j) j++;
k=0;
}
}
return min(i,j);
}
int main() {
read(T);
while(T--) {
scanf("%s",s);
n=strlen(s);
printf("%d\n",solve()+1);
}
return 0;
}