这个听的黄dalao过的。。
首先这个序列直接丢进map怕是要GG。。。看来只能自己手动Hash。。
构造一个具有可加性的hash函数,差不多像(ax+b)%mod这种。。然后去头和加尾都是十分容易操作。。
然而单hash就WA了。。调了好多参都没过。。
改成双Hash一发AC。。。
学到的东西就是多项式Hash和双Hash这两种姿势。。。
然而貌似kmp找循环节就可以了。。这个交给万能的汪聚聚。。
#include<bits/stdc++.h>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge*j=h[x];j;j=j->next)
#define succ(x) (1LL<<(x))
#define sqr(x) ((x)*(x))
#define mem(a) memset(a,0,sizeof(a))
#define lowbit(x) (x&(-x))
#define ll long long
#define moid (x+y>>1)
#define eps 1e-8
#define MAXN 1000005
using namespace std;
ll read(){
ll x=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return x*f;
}
int n,tot;
typedef struct tmp{
ll t1,t2;int t3;
friend bool operator<(tmp aa,tmp bb){
if(aa.t1!=bb.t1)return aa.t1<bb.t1;
if(aa.t2!=bb.t2)return aa.t2<bb.t2;
return aa.t3<bb.t3;
}
}tmp;
tmp c[MAXN];
//pair<int,ll>c[MAXN];
ll x,_t,_t1,y;
const ll t1=21841;
const ll inf1=290182597;
const ll t=1007;
const ll inf=1e9+7;
char s[2*MAXN];
vector<int>vec[MAXN];
typedef struct node{
int t1,t2,id;
friend bool operator<(node aa,node bb){
if(aa.t1==bb.t1)return aa.t2<bb.t2;
return aa.t1<bb.t1;
}
}node;
node d[MAXN];
int main(){
scanf("%s",s+1);
n=strlen(s+1);
inc(i,1,n)s[i+n]=s[i];
x=0;
inc(i,1,n){
x=(x*t+s[i])%inf;
y=(y*t1+sqr(s[i]))%inf1;
}
c[++tot]=(tmp){x,y,0};_t=_t1=1;
inc(i,1,n-1)_t=_t*t%inf,_t1=_t1*t1%inf1;
inc(i,n+1,2*n-1){
x-=_t*s[i]%inf;x%=inf;x=(x+inf)%inf;
y-=_t1*sqr(s[i])%inf1;y%=inf1;y=(y+inf1)%inf1;
x=(x*t+s[i])%inf;
y=(y*t1+sqr(s[i]))%inf1;
c[++tot]=(tmp){x,y,i-n};
}
sort(c+1,c+1+tot);int cnt=0;
ll last=-1,last1=-1;
inc(i,1,n){
if(c[i].t1==last&&last1==c[i].t2)vec[cnt].push_back(c[i].t3);
else vec[++cnt].push_back(c[i].t3);
last=c[i].t1;last1=c[i].t2;
}
//or(int i=1;i<=tot;i++)cout<<c[i].t1<<" "<<c[i].t2<<endl;
for(int i=1;i<=cnt;i++)d[i].t1=vec[i].size(),d[i].t2=vec[i][0],d[i].id=i;
sort(d+1,d+cnt+1);
printf("%d\n",cnt);
for(int i=1;i<=cnt;i++){
printf("%d",vec[d[i].id].size());
for(int j=0;j<vec[d[i].id].size();j++)printf(" %d",vec[d[i].id][j]);
printf("\n");
}
return 0;
}
Sort String
链接:https://www.nowcoder.com/acm/contest/141/E
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld
题目描述
Eddy likes to play with string which is a sequence of characters. One day, Eddy has played with a string S for a long time and wonders how could make it more enjoyable. Eddy comes up with following procedure:
1. For each i in [0,|S|-1], let Si be the substring of S starting from i-th character to the end followed by the substring of first i characters of S. Index of string starts from 0.
2. Group up all the Si. Si and Sj will be the same group if and only if Si=Sj.
3. For each group, let Lj be the list of index i in non-decreasing order of Si in this group.
4. Sort all the Lj by lexicographical order.
Eddy can't find any efficient way to compute the final result. As one of his best friend, you come to help him compute the answer!
输入描述:
Input contains only one line consisting of a string S. 1≤ |S|≤ 106 S only contains lowercase English letters(i.e. ).
输出描述:
First, output one line containing an integer K indicating the number of lists. For each following K lines, output each list in lexicographical order. For each list, output its length followed by the indexes in it separated by a single space.
示例1
输入
复制
abab
输出
复制
2 2 0 2 2 1 3
示例2
输入
复制
deadbeef
输出
复制
8 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7