牛客网暑期ACM多校训练营(第三场) Sort String

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

时间限制: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
#pragma GCC optimize ("O3")
#include<bits/stdc++.h>
#define LL long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define lson o<<1
#define rson o<<1|1
using namespace std;
const ull p = 131;
string s;
int len, res;
unordered_map<ull, int> sp;
ull has[2000006];
vector<int> V[2000006];
ull a[2000006];
void init(int n)
{
	a[0] = 1;
	for(int i = 1; i <= n; i++)
		a[i] = a[i - 1] * p;
}
void getsub(int len)
{
	res = 0;
	init(len);
	has[0] = 1;
	for(int i = 1; i < 2 * len; i++)
	{
		has[i] = has[i - 1] * p + s[i] - 'a';
		if(i >= len) 
		{
			ull k = has[i] - has[i - len] * a[len];
			if(!sp[k])
			{
				res++;
				sp[k] = res;
			}
			V[sp[k]].push_back(i - len);
		}
	}
}
int main()
{
	cin >> s;
	len = s.size();
	s += s;
	getsub(len);
	printf("%d\n", res);
	for(int i = 1; i <= res; i++)
	{
		printf("%d ", V[i].size());
		for(int j = 0; j < V[i].size(); j++)
			printf("%d%c", V[i][j], j == V[i].size() - 1?'\n' : ' ');
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/gtuif/article/details/81674012
今日推荐