题意是一个字符串,问从第几个字母开始,构成的字符串按字典序排列最小。可用最小表示法和后缀自动机做,时间复杂度都是O(n).
后缀自动机:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <bitset>
#define INF 0x3f3f3f3f
#define eps 1e-6
#define PI 3.1415926
#define mod 1000000009
#define base 2333
using namespace std;
typedef long long LL;
const int maxn = 1e4 + 10;
const int maxx = 1e3 + 10;
inline void splay(int &v) {
v=0;char c=0;int p=1;
while(c<'0' || c >'9'){if(c=='-')p=-1;c=getchar();}
while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();}
v*=p;
}
struct Node {
Node *pre, *nxt[26];
int step;
void Clear() {
step = 0;
pre = NULL;
memset(nxt, NULL, sizeof(nxt));
}
} *root, *last;
Node pool[maxn<<1], *cur;
int t;
char str[maxn];
void init() {
cur = pool;
root = last = cur++;
root->Clear();
}
void extend(int w) {
Node *np = cur++, *p = last;
np->Clear();
np->step = p->step+1;
while(p && !p->nxt[w])
p->nxt[w] = np, p = p->pre;
if(p == NULL)
np->pre = root;
else {
Node *q = p->nxt[w];
if(q->step == p->step+1)
np->pre = q;
else {
Node *nq = cur++;
memcpy(nq->nxt, q->nxt, sizeof(q->nxt));
nq->pre = q->pre;
nq->step = p->step+1;
np->pre = q->pre = nq;
while(p && p->nxt[w] == q)
p->nxt[w] = nq, p = p->pre;
}
}
last = np;
}
void solve() {
scanf("%d", &t);
while(t--) {
scanf("%s", str);
init();
int len = strlen(str);
for(int i = 0; i < len; i++)
extend(str[i]-'a');
for(int i = 0; i < len; i++)
extend(str[i]-'a');
Node *now = root;
for(int i = 0; i < len; i++) {
for(int j = 0; j < 26; j++) {
if(now->nxt[j] != NULL) {
now = now->nxt[j];
break;
}
}
}
printf("%d\n", now->step-len+1);
}
}
int main() {
//srand(time(NULL));
//freopen("kingdom.in","r",stdin);
//freopen("kingdom.out","w",stdout);
solve();
}
最小表示法:
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <iostream>
#define INF 0x3f3f3f3f
#define eps 1e-8
#define PI 3.1415926
#define mod 1000000007
typedef long long LL;
using namespace std;
const int maxn = 1e4+10;
int t, n;
char a[maxn];
int Min() {
int i = 0, j = 1, k = 0, t;
n = strlen(a);
while(i < n && j < n && k < n) {
t = a[(i+k)%n]-a[(j+k)%n];
if(t == 0)
k++;
else {
if(t > 0) i = i+k+1;
else j = j+k+1;
if(i == j) j++;
k = 0;
}
}
return min(i, j);
}
void solve() {
scanf("%d", &t);
while(t--) {
scanf("%s", a);
printf("%d\n", Min()+1);
}
}
int main() {
solve();
}