• 牛客第三场多校 E Sort String


    链接:https://www.nowcoder.com/acm/contest/141/E
    来源:牛客网

    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|≤ 10

    6


    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

    题意:就是说把一个字符串分别再每一个位置,把前缀放到后缀后面组成新的字符串,然后把相同字符串分入一个组,组里面的字符串索引按字典序排序
    例如实例一
    abcb在每个位置分别组成

    abab+""=abab
    bab+"a"=baba
    ab+"ab"=abab
    b+"aba"=baba
    由此我们可以看出(0,2)(1,3)分别组成一组

    然后我们可以仔细想想,只要字符串有一个字符不构成循环就会变成每个位置各在一组
    例如 ababac
    ababac+""=ababac
    babac+"a"=babaca
    abac+"ab"=abacab
    bac+"aba"=bacaba
    ac+"abab"=acabab
    c+"ababa"=cababa
    没有一个相同

    我们就会发现只有构成了字符循环才会有可能有多个字符串分在同一个组,而且还是有规律的,
    如abcabc abc为最小循环节,三个一循环,那么我们的组就有三个组,两个循环,说明一个组里面有两个成员,这个自己写几个例子就会明白
    然后我连最小循环节都说出来了,相信大家也明白了,使用kmp求next数组求出循环节
    #include<cstdio>
    #include<cstring>
    using namespace std;
    int len;
    int next[1000001];
    char s[1000001];
    int getnext(char s[])//求出next数组
    {
         
        int i=0,j=-1;
        next[0]=-1;
        while(i<len)
        {
            if(j==-1||s[i]==s[j])
            {
                next[++i]=++j;
            }
            else j=next[j];
        }
        return next[len];
    }
    int main()
    {
        int n;
        scanf("%s",s);
        len=strlen(s);
        int m=len-getnext(s);//循环节的长度
        if(len==m)
        {
            printf("%d
    ",len);
            for(int i=0;i<len;i++)
            {
                printf("1 %d
    ",i);
            }
        }
        else
        { if(len%m==0)//判断是否构成了循环
          {
            printf("%d
    ",m);
            for(int i=0;i<m;i++)
            {
                printf("%d",len/m);
                for(int j=i;j<len;j+=m)
                {
                    printf(" %d",j);
                }
                printf("
    ");
            }
          }
          else {
            printf("%d
    ",len);
            for(int i=0;i<len;i++)
            {
                printf("1 %d
    ",i);
            }
          }
        }
    }
  • 相关阅读:
    golang-cron定时任务
    卡特兰数
    树的直径
    虚拟机之Hyper-V
    tag of loj
    wx.requestSubscribeMessage无法弹窗,显示20001报错?
    nginx日志切割
    小程序首次加载过慢以及点击微信授权不弹出授权框的问题
    外网访问小程序显示网络错误问题以及总是走wx.request里面的fail回调问题
    http转https以及ssl证书配置以及安装
  • 原文地址:https://www.cnblogs.com/Lis-/p/9374719.html
Copyright © 2020-2023  润新知