题目大意:
就是现在对于一个字符串S, 每次都可以将第一个字符放到最后面, 这样一共有lenght(S)中串, 求这样的床中字典序最小的那个的起始位置是第几个字符时最小
大致思路:
首先后缀数组的做法不难想到, 就是将S变成SS之后求一遍后缀数组, 扫一遍height数组, 找到第一个sa[i] < len && sa[i] >= 0的那个sa[i],然后对于从这个位置开始的连续的height[i] >= length(S)的一段sa求出最小的即可, 细节可见代码
如果用后缀自动机来做的话, 将SS建立后缀自动机, 然后考虑后缀自动机上每一个状态的Right集合, 显然出现位置最早的就是right集合当中最小的那个位置作为结尾的位置
那么对于每一个状态都求出Right集合当中最小的那个即可, dfs一遍自动机, 在上面每次沿着字典序最小的那条边走, 走到length(S)步然后取Right中最小的作为结尾位置即可
代码如下:
后缀数组的做法:
Result : Accepted Memory : 1172 KB Time : 32 ms
/* * Author: Gatevin * Created Time: 2015/4/11 13:42:29 * File Name: Rin_Tohsaka.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e) #define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endl #define maxn 20100 int wa[maxn], wb[maxn], wv[maxn], Ws[maxn]; int cmp(int *r, int a, int b, int l) { return r[a] == r[b] && r[a + l] == r[b + l]; } void da(int *r, int *sa, int n, int m) { int *x = wa, *y = wb, *t, i, j, p; for(i = 0; i < m; i++) Ws[i] = 0; for(i = 0; i < n; i++) Ws[x[i] = r[i]]++; for(i = 1; i < m; i++) Ws[i] += Ws[i - 1]; for(i = n - 1; i >= 0; i--) sa[--Ws[x[i]]] = i; for(j = 1, p = 1; p < n; j <<= 1, m = p) { for(p = 0, i = n - j; i < n; i++) y[p++] = i; for(i = 0; i < n; i++) if(sa[i] >= j) y[p++] = sa[i] - j; for(i = 0; i < n; i++) wv[i] = x[y[i]]; for(i = 0; i < m; i++) Ws[i] = 0; for(i = 0; i < n; i++) Ws[wv[i]]++; for(i = 1; i < m; i++) Ws[i] += Ws[i - 1]; for(i = n - 1; i >= 0; i--) sa[--Ws[wv[i]]] = y[i]; for(t = x, x = y, y = t, p = 1, x[sa[0]] = 0, i = 1; i < n; i++) x[sa[i]] = cmp(y, sa[i - 1], sa[i], j) ? p - 1 : p++; } return; } int rank[maxn], height[maxn]; void calheight(int *r, int *sa, int n) { int i, j, k = 0; for(i = 1; i <= n; i++) rank[sa[i]] = i; for(i = 0; i < n; height[rank[i++]] = k) for(k ? k-- : 0, j = sa[rank[i] - 1]; r[i + k] == r[j + k]; k++); return; } char in[maxn]; int s[maxn], sa[maxn]; int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", in); int len = strlen(in); for(int i = 0; i < len; i++) s[i] = s[i + len] = in[i] - 'a' + 1; int n = 2*len; s[n] = 0; da(s, sa, n + 1, 27); calheight(s, sa, n); for(int i = 1; i <= n; i++) if(sa[i] < len) { int ans = sa[i]; while(i + 1 <= n && height[i + 1] >= len)//找出(长度len)字典序相同但出现位置最前面的 ans = min(ans, sa[i + 1]), i++; printf("%d\n", ans + 1); break; } } return 0; }
后缀自动机的做法:
Result : Accepted Memory : 3448 KB Time : 32 ms
/* * Author: Gatevin * Created Time: 2015/4/11 14:11:41 * File Name: Rin_Tohsaka.cpp */ #include<iostream> #include<sstream> #include<fstream> #include<vector> #include<list> #include<deque> #include<queue> #include<stack> #include<map> #include<set> #include<bitset> #include<algorithm> #include<cstdio> #include<cstdlib> #include<cstring> #include<cctype> #include<cmath> #include<ctime> #include<iomanip> using namespace std; const double eps(1e-8); typedef long long lint; #define foreach(e, x) for(__typeof(x.begin()) e = x.begin(); e != x.end(); ++e) #define SHOW_MEMORY(x) cout<<sizeof(x)/(1024*1024.)<<"MB"<<endl #define maxm 20010 #define maxn 40010 struct Suffix_Automation { struct State { State *par; State *go[26]; int val, mi, right, cnt; int minRight; void init(int _val = 0) { par = 0, val = _val, mi = right = cnt = 0; minRight = maxm; memset(go, 0, sizeof(go)); } int calc() { if(par == 0) return 0; else return val - par->val; } }; State *root, *last, *cur; State nodePool[maxn]; State* newState(int val = 0) { cur->init(val); return cur++; } void initSAM() { cur = nodePool; root = newState(); last = root; } void extend(int w, int pos) { State *p = last; State *np = newState(p->val + 1); np->right = 1; np->minRight = pos + 1; while(p && p->go[w] == 0) { p->go[w] = np; p = p->par; } if(p == 0) { np->par = root; } else { State *q = p->go[w]; if(q->val == p->val + 1) { np->par = q; } else { State *nq = newState(p->val + 1); memcpy(nq->go, q->go, sizeof(q->go)); nq->par = q->par; q->par = nq; np->par = nq; while(p && p->go[w] == q) { p->go[w] = nq; p = p->par; } } } last = np; } int d[maxm]; State* b[maxn]; void topo() { int maxVal = 0; memset(d, 0, sizeof(d)); int cnt = cur - nodePool; for(int i = 1; i < cnt; i++) maxVal = max(maxVal, nodePool[i].val), d[nodePool[i].val]++; for(int i = 1; i <= maxVal; i++) d[i] += d[i - 1]; for(int i = 1; i < cnt; i++) b[d[nodePool[i].val]--] = &nodePool[i]; b[0] = root; } void SAMInfo() { State *p; int cnt = cur - nodePool; for(int i = cnt - 1; i > 0; i--) { p = b[i]; p->par->right += p->right; p->mi = p->par->val + 1; p->par->minRight = min(p->par->minRight, p->minRight); } } bool dfs(State *now, int L, int len) { if(L == len) { printf("%d\n", now->minRight - len + 1); return true; } for(int i = 0; i < 26; i++) if(now->go[i]) if(dfs(now->go[i], L + 1, len)) return true; return false; } }; Suffix_Automation sam; char s[maxm]; int main() { int T; scanf("%d", &T); while(T--) { scanf("%s", s); int len = strlen(s); for(int i = 0; i < len; i++) s[len + i] = s[i]; sam.initSAM(); for(int i = 0; i < 2*len; i++) sam.extend(s[i] - 'a', i); sam.topo(); sam.SAMInfo(); sam.dfs(sam.root, 0, len); } return 0; }