链接: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 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. S and S will be the same group if and only if S=S.
3. For each group, let L be the list of index i in non-decreasing order of S in this group.
4. Sort all the L 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
S only contains lowercase English letters(i.e. 

牛客第三场多校 E Sort String-LMLPHP

).

输出描述:

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.
输入例子:
abab
输出例子:
2
2 0 2
2 1 3

-->

示例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[];
char s[];
int getnext(char s[])//求出next数组
{ int i=,j=-;
next[]=-;
while(i<len)
{
if(j==-||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=;i<len;i++)
{
printf("1 %d\n",i);
}
}
else
{ if(len%m==)//判断是否构成了循环
{
printf("%d\n",m);
for(int i=;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=;i<len;i++)
{
printf("1 %d\n",i);
}
}
}
}
05-11 02:53
查看更多