E-Sort String

题目链接: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:

  1. 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.
  2. Group up all the SiSi. SiSi and SjSj will be the same group if and only if Si=SjSi=Sj.
  3. For each group, let LjLj be the list of index ii in non-decreasing order of SiSi in this group.
  4. 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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值