牛客第三场多校 E 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 S i 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 S i. S i and S j will be the same group if and only if S i=S j.
3. For each group, let L j be the list of index i in non-decreasing order of S i in this group.
4. Sort all the L j 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|≤ 10

6


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

题意:就是说把一个字符串分别再每一个位置,把前缀放到后缀后面组成新的字符串,然后把相同字符串分入一个组,组里面的字符串索引按字典序排序
例如实例一
abcb在每个位置分别组成

abab+""=abab
bab+"a"=baba
ab+"ab"=abab
b+"aba"=baba
由此我们可以看出(0,2)(1,3)分别组成一组

然后我们可以仔细想想,只要字符串有一个字符不构成循环就会变成每个位置各在一组
例如 ababac
ababac+""=ababac
babac+"a"=babaca
abac+"ab"=abacab
bac+"aba"=bacaba
ac+"abab"=acabab
c+"ababa"=cababa
没有一个相同

我们就会发现只有构成了字符循环才会有可能有多个字符串分在同一个组,而且还是有规律的,
如abcabc abc为最小循环节,三个一循环,那么我们的组就有三个组,两个循环,说明一个组里面有两个成员,这个自己写几个例子就会明白
然后我连最小循环节都说出来了,相信大家也明白了,使用kmp求next数组求出循环节
#include<cstdio>
#include<cstring>
using namespace std;
int len;
int next[1000001];
char s[1000001];
int getnext(char s[])//求出next数组
{
     
    int i=0,j=-1;
    next[0]=-1;
    while(i<len)
    {
        if(j==-1||s[i]==s[j])
        {
            next[++i]=++j;
        }
        else j=next[j];
    }
    return next[len];
}
int main()
{
    int n;
    scanf("%s",s);
    len=strlen(s);
    int m=len-getnext(s);//循环节的长度
    if(len==m)
    {
        printf("%d\n",len);
        for(int i=0;i<len;i++)
        {
            printf("1 %d\n",i);
        }
    }
    else
    { if(len%m==0)//判断是否构成了循环
      {
        printf("%d\n",m);
        for(int i=0;i<m;i++)
        {
            printf("%d",len/m);
            for(int j=i;j<len;j+=m)
            {
                printf(" %d",j);
            }
            printf("\n");
        }
      }
      else {
        printf("%d\n",len);
        for(int i=0;i<len;i++)
        {
            printf("1 %d\n",i);
        }
      }
    }
}

 

转载于:https://www.cnblogs.com/Lis-/p/9374719.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值