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

题意:

将第一个字母移到最后,看有几种排序

分析:

用kmp求出循环节的长度len,然后判断n是否整除len,

代码:

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

const int maxn =1e6+10;
int next1[maxn];     /// 串的next1数组 (看这个串的相似度)
char s1[maxn];

void kmp_pre(char x[],int m,int next1[])
{
    int i,j;
    j=next1[0]=-1;
    i=0;
    while(i<m){
        while(-1!=j&&x[i]!=x[j])j=next1[j];
        next1[++i]=++j;
    }
}
int main()
{
        scanf("%s",s1);
        int len=strlen(s1);
        kmp_pre(s1,len,next1);
        int temp=len-next1[len];
        if(len%temp!=0)
        {
        temp=len;
        printf("%d\n",temp);
        for(int i=0;i<temp;i++)
           {
             printf("%d %d",len/temp,i);
             for(int j=1;j<len/temp;j++)
                printf(" %d",i+j*temp);
             printf("\n");
           }
        }
        else{
             printf("%d\n",temp);
        for(int i=0;i<temp;i++)
           {
             printf("%d %d",len/temp,i);
             for(int j=1;j<len/temp;j++)
                printf(" %d",i+j*temp);
             printf("\n");
           }
        }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值