2018牛客多校训练---Sort String

链接:https://www.nowcoder.com/acm/contest/141/E
来源:牛客网
 

题目描述

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

  根据原字符串构造新串,对于原字符串从0~|S|-1,i从0开始,从i到最后的子串放到从0到i-1子串的前面。对于这些新构造的子串,从0开始编号,将相同的新字符串们归为一组,输出一共有多少组,每组有多少个新字符串,以及新字符串的编号。

 思路:

  写几个字符串后会发现只有字符串存在循环子串情况下,才会出现相同的字符串,才会出现多个字符串同组情况。利用kmp求出失配数组next[i],表示前i个字符串最长前缀和后缀的长度。求出循环子串的长度,根据循环子串判断并输出分组情况。

#include<bits/stdc++.h>
using namespace std;

string P,s;//模板串
int f[1000015];

void getFail()//得到失配数组
{
    int m =P.length();
    f[0]=f[1]=0;
    for(int i=1;i<m;i++)
    {
        int j=f[i];
        while(j && P[i]!=P[j]) j=f[j];
        f[i+1]=(P[i]==P[j])?j+1:0;
    }
}

int main()
{
    cin>>s;
    P=s;
    getFail();
    int i,j;
    int n=P.length();
    int l=n-f[n];
    bool ff=0;
    if(n%l)
        ff=1;
    if(ff)
        l=n;
    printf("%d\n",l);
    for(i=0;i<l;i++)
    {
        printf("%d %d",n/l,i);
        for(j=1;j<n/l;j++)
            printf(" %d",i+l*j);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jinghui_7/article/details/81227094
今日推荐