这题可用后缀自动机,当然最小表示法更简单。。。
#include <iostream>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <time.h>
#define maxn 100005
#define maxm 300006
#define eps 1e-10
#define mod 1315423911
#define INF 1e9
#define lowbit(x) (x&(-x))
#define mp make_pair
#define ls o<<1
#define rs o<<1 | 1
#define lson o<<1, L, mid
#define rson o<<1 | 1, mid+1, R
typedef long long LL;
typedef unsigned long long ULL;
//typedef int LL;
using namespace std;
char s[maxn];
void read(void)
{
scanf("%s", s);
}
void work(void)
{
int i = 0, j = 1;
int len = strlen(s);
for(int k = 0; k != len;) {
if(i == j) j++;
int ni = (i + k) % len;
int nj = (j + k) % len;
if(s[ni] < s[nj]) j += k+1, k = 0;
else if(s[nj] < s[ni]) i += k+1, k = 0;
else k++;
}
printf("%d\n", i+1);
}
int main(void)
{
int _;
while(scanf("%d", &_)!=EOF) {
while(_--) {
read();
work();
}
}
return 0;
}