Sort String 2018牛客多校第三场 E题 (字符串hash)

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 S i=Sj.
3. For each group, let Lj be the list of index i in non-decreasing order of S i 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

 

题意:问每次把前i个字符放到字符串最后,问可形成多少个字符串.

思路:可以用KMP求循环节的方法来做.这里记录一种字符串hash方法,即把两个相同的字符串接起来,然后每次取原串长度,依次后移,把此子串的hash值求出来,最后看有几种hash值即可.

哈希可用单哈希或双哈希,单哈希不设置mod(模)的话,必须用无符号整数,模的话模大素数即可.但二者都有一定可能产生冲突,这个出题人可能会卡.

双哈希就很难被卡,设置两个差值较小的大素数做mod,产生冲突的概率极低.

本题单哈希有模过不了,没模可过,双哈希可过.

两个代码如下:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define mod 1000000009
using namespace std;
typedef unsigned long long ll;
const int maxn = 1e6+5;
const double eps = 1e-12;
const int inf = 0x3f3f3f3f;
map<int,int>::iterator it;

struct node
{
	int pos;
	ll val;
} a[maxn];

ll bas = 131;//选素数
char s[maxn<<1],t[maxn];
vector<int> ans[maxn];

bool cmp(node x,node y)
{
	if(x.val == y.val) return x.pos< y.pos;
	return x.val< y.val;
}

int main()
{
	scanf(" %s",t);
	strcpy(s,t);
	strcat(s,t);
	
	int len = strlen(s),cnt = 0;
	ll ht = 1,cur = 0;
	len/= 2;
	for(int i = 1;i<= len;i++) ht = ht*bas;//求len次幂
	for(int i = 0;i< len+len;i++)
	{
		cur = cur*bas+s[i]-'a'+1;
		if(i>= len)
			cur = (cur-ht*(s[i-len]-'a'+1));//计算[i-len+1,i]区间的哈希值
		if(i>= len)
		{
			int now = i-len;
			a[now+1].pos = now;
			a[now+1].val = cur;
		}
	}
	
    //以下为处理出答案
	sort(a+1,a+len+1,cmp);
	
	int now ;
	a[0].val = -1;
	for(int i = 1;i<= len;i++)
	{
		if(a[i].val != a[i-1].val)
		{
			cnt++;
			now = a[i].pos;
			ans[now].push_back(now);
			continue;
		}
		ans[now].push_back(a[i].pos);
	}
	
	printf("%d\n",cnt);
	for(int i = 0;i<= len+2;i++)
	{
		int k = ans[i].size();
		if(k == 0) continue;
		printf("%d",k);
		for(int j = 0;j< k;j++)
			printf(" %d",ans[i][j]);
		printf("\n");
	}
	
	return 0;
}

双哈希:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
const double eps = 1e-12;
const int inf = 0x3f3f3f3f;
map<int,int>::iterator it;

ll mod[3] = {0,1000000007,1000000009};//选两个素数
struct node
{
	ll v[3];
	node()
	{
		v[1] = v[2] = 0;
	}
	node(ll x)
	{
		v[1] = x;
		v[2] = x;
	}
	friend node operator * (node x,node y)
	{
		x.v[1] = x.v[1]*y.v[1];
		if(x.v[1]>= mod[1]) x.v[1]%= mod[1];//减少模的次数以免超时
		x.v[2] = x.v[2]*y.v[2];
		if(x.v[2]>= mod[2]) x.v[2]%= mod[2];
		return x; 
	}
	friend node operator - (node x,node y)
	{
		x.v[1] = x.v[1]-y.v[1];
		if(x.v[1]< 0) x.v[1] = x.v[1]+mod[1];
		x.v[2] = x.v[2]-y.v[2];
		if(x.v[2]< 0) x.v[2] = x.v[2]+mod[2];
		return x; 
	}
	friend node operator + (node x,node y)
	{
		x.v[1] = x.v[1]+y.v[1];
		if(x.v[1]>= mod[1]) x.v[1]%= mod[1];
		x.v[2] = x.v[2]+y.v[2];
		if(x.v[2]>= mod[2]) x.v[2]%= mod[2];
		return x; 
	}
};

struct P
{
	int pos;
	pair<ll,ll> val;
} a[maxn];

node bas(131);
char s[maxn<<1],t[maxn];
vector<int> ans[maxn];

bool cmp(P x,P y)
{
	if(x.val == y.val) return x.pos< y.pos;
	return x.val< y.val;
}

int main()
{
	scanf(" %s",t);
	strcpy(s,t);
	strcat(s,t);
	
	int len = strlen(s),cnt = 0;
	len>>= 1;
	node st(1),cur(0);
	for(int i = 1;i<= len;i++) st = st*bas;
	for(int i = 0;i< len+len;i++)
	{
		cur = cur*bas+node(s[i]-'a'+1);
		if(i>= len)
			cur = cur-st*node(s[i-len]-'a'+1);
		if(i>= len)
		{
			int now = i-len;
			a[now+1].pos = now;
			a[now+1].val.first = cur.v[1];
			a[now+1].val.second = cur.v[2];
		}
	}
	
	sort(a+1,a+len+1,cmp);
	
	int now;
	a[0].val.first = -1;
	for(int i = 1;i<= len;i++)
	{
		if(a[i].val != a[i-1].val)
		{
			cnt++;
			now = a[i].pos;
			ans[now].push_back(now);
			continue;
		}
		ans[now].push_back(a[i].pos);
	}
	
	printf("%d\n",cnt);
	for(int i = 0;i<= len;i++)
	{
		int k = ans[i].size();
		if(k == 0) continue;
		printf("%d",k);
		for(int j = 0;j< k;j++)
			printf(" %d",ans[i][j]);
		printf("\n");
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值