题目链接:https://ac.nowcoder.com/acm/problem/17244
Eddy likes to play with string which is a sequence of characters. One day, Eddy has played with a string SS for a long time and wonders how could make it more enjoyable. Eddy comes up with following procedure:
- For each ii in [0,|S|−1][0,|S|−1], let SiSi be the substring of SS starting from ii-th character to the end followed by the substring of first ii characters of SS. Index of string starts from 00.
- Group up all the SiSi. SiSi and SjSj will be the same group if and only if Si=SjSi=Sj.
- For each group, let LjLj be the list of index ii in non-decreasing order of SiSi in this group.
- Sort all the LjLj 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
Input contains only one line consisting of a string SS.
1≤|S|≤1061≤|S|≤106
SS only contains lowercase English letters(i.e. "a−za−z").
Output
First, output one line containing an integer KK indicating the number of lists.
For each following KK lines, output each list in lexicographical order.
For each list, output its length followed by the indexes in it separated by a single space.
Examples
Input
abab
Output
2 2 0 2 2 1 3
Input
deadbeef
Output
8 1 0 1 1 1 2 1 3 1 4 1 5 1 6 1 7
题意:给你一个长度为n的字符串,可以认为是环形,定义字符串si(i>=0,i<len),都是从i开始长度为len,将相同的字符串分成一组,问有多少组字符串;以字典序输出
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+100;
typedef unsigned long long ull;
ull f=233317;
unordered_map<ull,int>mp;
vector<int>v[maxn];
int tot;
ull Hash[maxn*2];
char s[maxn*2];
inline void write( int x )
{
if ( x<0 ) putchar('-'),x=-x;
if ( x>9 ) write(x/10);
putchar(x%10+'0');
}
int main()
{
scanf("%s",s+1);s[0]='#';
int len=strlen(s)-1;
Hash[0]=0;
for(int i=1;i<=len;i++) Hash[i]=Hash[i-1]*f+s[i];
for(int i=len+1;i<=len*2;i++) s[i]=s[i-len];
ull need=1;
for(int i=1;i<len;i++) need*=f;
for(int i=len+1;i<=len*2;i++) Hash[i]=(Hash[i-1]-s[i-len]*need)*f+s[i];
for(int i=len;i<=len*2-1;i++)
{
if(!mp[Hash[i]])
{
mp[Hash[i]]=++tot;
v[tot].push_back(i-len);
}
else v[mp[Hash[i]]].push_back(i-len);
}
cout<<tot<<endl;
for(int i=1;i<=tot;i++)
{
int now=v[i].size();
write(now);
cout<<" ";
for(int j=0;j<now;j++)
{
write(v[i][j]);
printf(" ");
}
printf("\n");
}
return 0;
}