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

链接: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

题意:这道题的题意真是迷的不行。。。其实就是给你一个字符串,然后初始串编号为0,再依次从前往后拿一个字符放到后面组成新串,编号1~n-1。这些串里相同的串分为一组,求能分多少组,每组的串的编号是多少,编号从小到大,组按第一个串编号的字典序输出。

思考问题的本质。如果这个串是周期串,则组数为周期的长度,每组有总长度/周期长度个串。如果不是,则一定为n组,即样例的第二种情况。比赛的时候写了个错的kmp居然过了。。。数据比较水。。。下面是正确的代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e6+10;
int n,m,k;
int f[maxn];
char s[maxn];
void getFail()
{
    f[0]=f[1]=0;
    for(int i=1;i<m;i++)
    {
     int j=f[i];
     while(j&&s[i]!=s[j]) j=f[j];
     f[i+1]=(s[i]==s[j])?j+1:0;
    }
}
int main()
{
    int T,cas=1;
    scanf("%s",s);m=strlen(s);
    getFail();
    int len=m-f[m];
    bool jd=0;
    if(m%len)jd=1;
    if(jd) len=m;
    printf("%d\n",len);
    for(int i=0;i<len;i++)
    {
        printf("%d %d",m/len,i);
        for(int j=1;j<m/len;j++)
        {
            printf(" %d",i+len*j);
        }
        puts("");
    }
    return 0;
}

附上大牛hash卡过的代码,侵删

#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define mem(a,b) memset((a),(b),sizeof(a))
#define MP make_pair
#define pb push_back
#define fi first
#define se second
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
using namespace __gnu_cxx;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
const int INF=0x3f3f3f3f;
const ll LLINF=0x3f3f3f3f3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-6;
const int MAX=2e6+10;
const ll mod=1e9+7;
/*********************************  head  *********************************/
struct hash_table
{
    ull seed;
    ull Hash[MAX],tmp[MAX];
    void set(ull _seed)
    {
        seed=_seed;
    }
    void work(char *s,int n)
    {
        ll i,j;
        tmp[0]=1;
        Hash[0]=0;
        for(i=1;i<=n;i++) tmp[i]=tmp[i-1]*seed;
        for(i=1;i<=n;i++) Hash[i]=(Hash[i-1]*seed+(s[i]-'a'));
    }
    ull get(int l,int r)
    {
        return (Hash[r]-Hash[l-1]*tmp[r-l+1]);
    }
}h;
char s[MAX];
VI res[MAX];
int main()
{
    int i,j,len,tot;
    while(~scanf("%s",s+1))
    {
        len=strlen(s+1);
        for(i=len+1,j=1;j<=len;i++,j++) s[i]=s[j];
        h.set(23333);
        h.work(s,len*2);
        unordered_map<ull,int> mp;
        tot=0;
        for(i=1;i<=len;i++)
        {
            ull tmp=h.get(i,i+len-1);
            if(!mp.count(tmp)) mp[tmp]=++tot;
            res[mp[tmp]].pb(i-1);
        }
        printf("%d\n",tot);
        for(i=1;i<=tot;i++)
        {
            printf("%d",sz(res[i]));
            for(auto it:res[i]) printf(" %d",it);
            puts("");
        }
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值