zoukankan      html  css  js  c++  java
  • E Sort String(字符串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:
    
    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.
    Group up all the Si. Si and Sj will be the same group if and only if Si=Sj.
    For each group, let Lj be the list of index i in non-decreasing order of Si in this group.
    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
    Input contains only one line consisting of a string S.
    
    1≤|S|≤106
    S only contains lowercase English letters(i.e. "a−z").
    
    Output
    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.
    
    Examples
    Input
    abab
    Output
    2
    2 0 2
    2 1 3
    Input
    deadbeef
    Output
    8
    1 0
    1 1
    1 2
    1 3
    1 4
    1 5
    1 6
    1 7
    题目

    题目大意:输入一个字符串,字符串长度为len,从字符串的第i位开始取len个字符(不够的话从头取)

    有几个不同的字符串。

    题解:字符串Hash

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    
    typedef unsigned long long ULL;
    const int seed=131;
    const int N=2000002;
    const int M=1000001;
    char s[N];
    int js;
    ULL p[N],h[N];
    vector<int>a[M];
    unordered_map<ULL,int>mp;
    
    void Hash(int x)
    {
        p[0]=1;
        for(int i=1;i<=x;i++)
        {
            p[i]=p[i-1]*seed;
            h[i]=h[i-1]*seed+s[i];
        }
    }
    
    ULL get_Hash(int l,int r)
    {
        return h[r]-h[l-1]*p[r-l+1];
    }
    
    int main()
    {
        scanf("%s",s+1);
        int len=strlen(s+1);
        for(int i=len+1;i<=len+len;i++) s[i]=s[i-len];
        Hash(len*2);
        for(int i=1;i<=len;i++)
        {
            ULL tmp=get_Hash(i,i+len-1);
            if(!mp[tmp])
            {
                mp[tmp]=++js;
            }
            a[mp[tmp]].push_back(i-1);
        }
        printf("%d
    ",js);
        for(int i=1;i<=js;i++)
        {
            int sz=a[i].size();
            printf("%d",sz);
            for(int j=0;j<sz;j++) printf(" %d",a[i][j]);
            printf("
    ");
        }
        return 0;
    }
  • 相关阅读:
    解题报告 poj 1486
    解题报告 比赛
    解题报告 keke 的房子
    解题报告 Tree
    解题报告 聚会
    解题报告 Valentine‘s seat
    解题报告 报数
    解题报告 黑书 Water pail poi 1999
    解题报告 poj 1639
    解题报告 数数
  • 原文地址:https://www.cnblogs.com/zzyh/p/14706330.html
Copyright © 2011-2022 走看看