• 牛客网多校第3场Esort string (kmp)


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

    字符串哈希超时,还是太菜看到大佬用unordered map写哈希过了,

    kmp 求循环节;

    #include <iostream>
    #include<stdio.h>
    #include<string.h>
    using namespace std;
    #define ll long long
    #define maxn 1000010
    char s[maxn];
    int len, Next[maxn];
    void makeNext(int tlen) {
        int j = 0, k = -1;
        Next[0] = -1;
        while (j < tlen) {
            if (k == -1 || s[j] == s[k]) {
                Next[++j] = ++k;
            } else {
                k = Next[k];
            }
        }
    }
    
    int main()
    {
        cin>>s;
        len=strlen(s);
        makeNext(len);
        int x=len-Next[len];
        if (len % x!= 0) {
            printf("%d",len);
            for(int i = 0; i < len; i++) {
                printf("1 %d
    ",i);
            }
        }
        else {
            printf("%d
    ",x);
            for(int i = 0; i < x; i++) {
                printf("%d",len/x);
                for(int j = i; j < len; j += x)
                    printf(" %d",j);
                printf("
    ");
            }
        }
        
        
        
        
        return 0;
    }
  • 相关阅读:
    从汇编看c++对静态成员的存取
    从汇编看c++内联函数评估求值
    从汇编看c++初始化列表初始化成员变量
    53. sql2005“备份集中的数据库备份与现有的xx数据库不同”解决方法
    52. 查看linux系统是32位还是64位
    51. linux卸载jdk
    50. linux下查看tomcat日志
    49. jdk-6u45-linux-i586.bin安装步骤
    48. Linux 删除文件夹命令
    47. linux下给已经存在的用户设置用户组
  • 原文地址:https://www.cnblogs.com/2014slx/p/9377220.html
Copyright © 2020-2023  润新知