牛客網暑期ACM多校訓練營(第三場) E Sort String

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

 

題意:給你一個字符串從0開始編號,si表示的是從第i位置開始到字符串結束和從0開始到i-1的字符串相連接組成的新串。

題目問有哪些串是相同,你就把他們字符串的編號放在一起。輸出的時候要求字典序最小的一組先輸出,每一列編號要求遞增。

解題思路:一開始比賽的時候題沒看明白,然後我看題解的時候發現這竟然還能用kmp做,真是服了我的腦子。kmp的話就是看這整個字符串是不是由一個字串循環得到,就像樣例1一樣,如果不行的話,就是每一個位置爲一組。

當時有668人過了這個題,但我還是寫出來,因爲我拿來練習一下哈希表。你沒有聽錯,這也能用哈希做。

不過自己的沒搞出來,借鑑了別人的思路後,對哈希表的理解有點長進。然後寫出來分享分享。

hash做法

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
using namespace std;
const int maxn=2e6+10;
#define mem(a,b) memset(a,b,sizeof(a))
#define ull unsigned long long
pair<ull,int> s[maxn];
vector<int> ve[maxn];
int pos[maxn],vis[maxn];
struct hash_table{
	ull key;
	ull hash[maxn],temp[maxn];
	void set(ull sum){
		key=sum;
	}
	void work(char *s,int n){
		temp[0]=1;hash[0]=0;
		for(int i=1;i<=n;i++) temp[i]=temp[i-1]*key;
		for(int i=1;i<=n;i++) hash[i]=hash[i-1]*key+s[i]-'a';
	}
	ull get(int l,int r){
		return hash[r]-hash[l-1]*temp[r-l+1];
	}
}h;
char ch[maxn];
bool cmp(pair<ull,int> &a,pair<ull,int> &b){
	if(a.first!=b.first) return a.first<b.first;
	return a.second<b.second;
}
int main(){
	int i,j;
	scanf("%s",ch+1);
	int len=strlen(ch+1),num=0;
	mem(vis,0);
	for(i=1;i<=len;i++){
		ch[len+i]=ch[i];ve[i].clear();
	}
	len;
	h.set(131);
	h.work(ch,len*2);
	for(i=1;i<=len;i++){
		ull tmp=h.get(i,i+len-1);
		s[i]=make_pair(tmp,i);
 	}
 	sort(s+1,s+len+1,cmp);
 	for(i=1;i<=len;i++){
 		pos[s[i].second]=i;
	}
	int tot=0;
	for(i=1;i<=len;i++){
		if(!vis[i]){
			for(j=pos[i];j<=len&&(j==pos[i]||s[j].first==s[j-1].first);j++){
				vis[s[j].second]=1;
				ve[tot].push_back(s[j].second);
			}
			tot++;
		}
	} 
 	printf("%d\n",tot);
	for(i=0;i<tot;i++){
		printf("%d",ve[i].size());
		for(j=0;j<ve[i].size();j++)
			printf(" %d",ve[i][j]-1);
		printf("\n");
	} 
	return 0;
}

 

KMP做法

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=1e6+1;
int nex[maxn],n,m;
char ch[maxn];
void find(){
    memset(nex,0,sizeof(nex));
    nex[0]=-1;
    int i=0,j=-1,len=strlen(ch);
    while(i<len){
        if(j==-1||ch[i]==ch[j]){
            nex[++i]=++j;
        }
        else j=nex[j];
    }
}
int main(){
    int i,j;
    scanf("%s",ch);
    find();
    int len=strlen(ch);
    int t=len-nex[len];
    if(len%t!=0){
        printf("%d\n",len);
        for(i=0;i<len;i++){
            printf("1 %d\n",i);
        }
    }
    else{
        int x=len/t;
        printf("%d\n",t);
        for(i=0;i<t;i++){
            printf("%d",x);
            for(j=i;j<len;j+=t){
                printf(" %d",j);
            }
            printf("\n");
        }
    }
    return 0;
}

繼續加油。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章