暑期多校第三场

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

Sort String

时间限制: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

题意:

这道题描述不是很清楚,简单说就是将从某一位开始一直到最后的子串挪到前面剩余字符串的前面

S(0)代表从第零位开始集体往前挪其实就是原串,这样一直到是S(len-1),字符串从0开始标号,问这些串中可以分成几组,每组的标号是多少输出各组,输出顺序按族中标号的字典序排序

题解;

解释一下循环节,abababab,这个留有很多循环接:ab,abab

最小循环节(串长度大小为len)用len-next[len]得出,next[]数组为kmp算法的next数组

这题分两种情况;

一种是这个串整个没有循环节,如abababa,就没有,必须是整串的循环节,没循环节就分成len组,每组都是一个

一种是这个串有循环节分组的个数就是循环节长度,每个组内个数就是len/循环节长度

代码;

#include<stdio.h>
#include<string.h>
using namespace std;
char a[1000002];
int alen=0;
int next[1000003];
void getnext()
{
    int i=0,j=-1;
    next[0]=-1;
     while(i<alen)
    {
        if(j==-1||a[i]==a[j])
        {
            i++;
            j++;
            next[i]=j;
            //printf("%d**\n",next[i]);
        }
        else
        {
            j=next[j];
        }
    }

}
int main()
{

    while(~scanf("%s",a))
    {
        alen=strlen(a);
       getnext();
       int m=alen-next[alen];
      if(alen%m==0)
       {
           printf("%d\n",m);
           for(int i=0;i<m;i++)
           {    int h=alen/m;
             printf("%d",h);
               for(int j=0;j<h;j++)
               {
                   printf(" %d",m*j+i);
               }
               printf("\n");
           }
       }
       else
       {
           printf("%d\n",alen);
           for(int i=0;i<alen;i++)
           {
               printf("1 %d\n",i);
           }
       }

    }
}

猜你喜欢

转载自blog.csdn.net/wrwhahah/article/details/81233299