牛客网暑期ACM多校训练营(第三场)E Sort String【循环节】

https://www.nowcoder.com/acm/contest/141/E

用后缀数组和哈希操作发现都会超时,后经发现答案跟循环节有关,只需用next数组找出循环节即可。

AC代码:

#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 1e6+66;
char s[maxn];
int len, Next[maxn];
void makeNext() {
    int i = 0, j = 0;
    Next[i] = j;
    for (i = 1; i < len; i++) {
        while (j && s[i] != s[j]) j = Next[j - 1];
        if (s[i] == s[j]) j++;
        Next[i] = j;
    }
}
int main() {
    scanf("%s",s);
    len = strlen(s);
    makeNext();
    int L = len - Next[len - 1];
    int ans=(len % L) ? 1 : len / L;
    int kind=len/ans;
    swap(kind,ans);
    printf("%d\n",ans);
    for(int i=1;i<=ans;i++){
        printf("%d",kind);
        for(int j=0;jprintf(" %d",i-1+j*ans);
        }
        printf("\n");

    }
    return 0;
}

哈希(TLE):

#include
using namespace std;
#define ll long long int
const int maxn=2e6+2333;
const ll p=1e9+7;
char a[maxn];
ll hashi[maxn];
ll tmp[maxn];
mapint> q;
vector ans[maxn];
ll qkm(ll a,ll b){
    ll ans=1;
    while(b>0){
        if(b%2){
            ans=ans*a;ans%=p;
        }
        a=a*a;a%=p;
        b>>=1;
    }
    (ans+=p)%=p;
    return ans;
}
void hasha(){
    ll len=strlen(a);
    tmp[0]=a[0]-'a';
    for(int i=1;i1]*131+1ll*(a[i]-'a')+p)%p;
    }
    hashi[0]=tmp[len-1];
    ll cs=qkm(131,len-1);
    for(int i=1;i1] - ((a[i-1]-'a')*cs)%p ) * 131 % p + (a[i-1]-'a') + p) % p;
    }
}
int main()
{
    scanf("%s",a);
    int len=strlen(a);
    int sz=0;
    hasha();
    for(int i=0;iif(q[hashi[i]]!=0){
            ans[q[hashi[i]]].push_back(i);
        }
        else{
            q[hashi[i]]=++sz;;
            ans[q[hashi[i]]].push_back(i);
        }
    }
    printf("%d\n",sz);
    for(int i=1;i<=sz;i++){
        printf("%d",ans[i].size());
        for(int j=0;jprintf(" %d",ans[i][j]);
        }
        printf("\n");
    }
    return 0;
}

后缀数组(TLE):


    while(p < len){
        for(int i = len - d; i < len; i++)
            id[p++] = i;
        rep(i,len)
        if(sa[i] >= d)
            id[p++] = sa[i] - d;
        rep(i,len) r[i] = k[id[i]];
        rep(i,up) cnt[i] = 0;
        rep(i,len) cnt[r[i]]++;
        rep(i,up) cnt[i+1] += cnt[i];
        for(int i = len - 1; i >= 0; i--) {
            sa[--cnt[r[i]]] = id[i];
        }
        swap(k,r);
        p = 0;
        k[sa[0]] = p++;
        rep(i,len-1) {
            if(sa[i]+d < len && sa[i+1]+d 1]]&& r[sa[i]+d] == r[sa[i+1]+d])
                k[sa[i+1]] = p - 1;
            else k[sa[i+1]] = p++;
        }
        if(p >= len) return ;
        d *= 2,up = p, p = 0;
    }
}
void getHeight(int len) {

    rep(i,len) rk[sa[i]] = i;
    height[0] = 0;
    for(int i = 0,p = 0; i < len - 1; i++) {
        int j = sa[rk[i]-1];
        while(i+p < len&& j+p < len&& w[i+p] == w[j+p]) {
            p++;
        }
        height[rk[i]] = p;
        p = max(0,p - 1);
    }
}
int getSuffix(char s[]) {
    int len = strlen(s),up = 0;
    for(int i = 0; i < len; i++) {
        w[i] = s[i];
        up = max(up,w[i]);
    }
    w[len++] = 0;
    getSa(len,up+1);
    getHeight(len);
    return len;
}

char s[maxn];

vector<vector<int> >a;

int vis[maxn];

int main()
{
    scanf("%s",s);
    int len=strlen(s);
    for(int i=0;i1;i++){
        s[len+i]=s[i];
    }
    s[len+len-1]=0;
  //  printf("%s\n",s);
    getSuffix(s);
    vector<int> b;
    for(int i=0;iint m=rk[i];
        if(vis[i]==1) continue;
        b.push_back(i);
        vis[i]=1;
        while(vis[m-1]==0&&height[m]>=len){
            b.push_back(sa[m-1]);
            vis[sa[m-1]]=1;
            m--;
        }
        a.push_back(b);
    }
    printf("%d\n",a.size());
    for(int i=0;iprintf("%d",a[i].size());
        for(int j=0;jprintf(" %d",a[i][j]);
        }
        printf("\n");
    }


    return 0;
}

你可能感兴趣的:(Data,Structure)