题意:
字符串i:suffix(i)+prefix(i-1).
把这0到n-1个字符串分组。完全一样的分为一组。
按照字符串标号,一组一组输出。
POINT:
找规律可得,只要找出最小循环节的长度,答案就确定了。
之前用后缀数组写的,然后tle,所以输出了一些字符串找到了规律。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int maxn = 1e6+66;
char s[maxn];
int len, Next[maxn];
void makeNext() {
int i = 0, j = 0;
Next[i] = j;
for (i = 1; i < len; i++) {
while (j && s[i] != s[j]) j = Next[j - 1];
if (s[i] == s[j]) j++;
Next[i] = j;
}
}
int main() {
scanf("%s",s);
len = strlen(s);
makeNext();
int L = len - Next[len - 1];
int ans=(len % L) ? 1 : len / L;
int kind=len/ans;
swap(kind,ans);
printf("%d\n",ans);
for(int i=1;i<=ans;i++){
printf("%d",kind);
for(int j=0;j<kind;j++){
printf(" %d",i-1+j*ans);
}
printf("\n");
}
return 0;
}
后缀数组(TLE)
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
#define rep(i,n) for(int i = 0;i < n; i++)
const int maxn = 2000000+66;
int rk[maxn],sa[maxn],height[maxn],w[maxn],wa[maxn],res[maxn];
void getSa (int len,int up) {
int *k = rk,*id = height,*r = res, *cnt = wa;
rep(i,up) cnt[i] = 0;
rep(i,len) cnt[k[i] = w[i]]++;
rep(i,up) cnt[i+1] += cnt[i];
for(int i = len - 1; i >= 0; i--) {
sa[--cnt[k[i]]] = i;
}
int d = 1,p = 0;
while(p < len){
for(int i = len - d; i < len; i++)
id[p++] = i;
rep(i,len)
if(sa[i] >= d)
id[p++] = sa[i] - d;
rep(i,len) r[i] = k[id[i]];
rep(i,up) cnt[i] = 0;
rep(i,len) cnt[r[i]]++;
rep(i,up) cnt[i+1] += cnt[i];
for(int i = len - 1; i >= 0; i--) {
sa[--cnt[r[i]]] = id[i];
}
swap(k,r);
p = 0;
k[sa[0]] = p++;
rep(i,len-1) {
if(sa[i]+d < len && sa[i+1]+d <len &&r[sa[i]] == r[sa[i+1]]&& r[sa[i]+d] == r[sa[i+1]+d])
k[sa[i+1]] = p - 1;
else k[sa[i+1]] = p++;
}
if(p >= len) return ;
d *= 2,up = p, p = 0;
}
}
void getHeight(int len) {
rep(i,len) rk[sa[i]] = i;
height[0] = 0;
for(int i = 0,p = 0; i < len - 1; i++) {
int j = sa[rk[i]-1];
while(i+p < len&& j+p < len&& w[i+p] == w[j+p]) {
p++;
}
height[rk[i]] = p;
p = max(0,p - 1);
}
}
int getSuffix(char s[]) {
int len = strlen(s),up = 0;
for(int i = 0; i < len; i++) {
w[i] = s[i];
up = max(up,w[i]);
}
w[len++] = 0;
getSa(len,up+1);
getHeight(len);
return len;
}
char s[maxn];
vector<vector<int> >a;
int vis[maxn];
int main()
{
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len-1;i++){
s[len+i]=s[i];
}
s[len+len-1]=0;
// printf("%s\n",s);
getSuffix(s);
vector<int> b;
for(int i=0;i<len;i++){
b.clear();
int m=rk[i];
if(vis[i]==1) continue;
b.push_back(i);
vis[i]=1;
while(vis[m-1]==0&&height[m]>=len){
b.push_back(sa[m-1]);
vis[sa[m-1]]=1;
m--;
}
a.push_back(b);
}
printf("%d\n",a.size());
for(int i=0;i<a.size();i++){
printf("%d",a[i].size());
for(int j=0;j<a[i].size();j++){
printf(" %d",a[i][j]);
}
printf("\n");
}
return 0;
}