zoukankan      html  css  js  c++  java
  • 牛客网 牛客网暑期ACM多校训练营(第三场)E 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|≤ 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

    题意   给定一个字符串T 设长度为n   然由这个字符串产生出n个字符串s0...sn-1  si=Ti-Tn-1+T0-Ti-1 将si相同的分到一组   i 按照从小到大排序 输出分组后的 i 序列

    解析   我们可以推导一下 若T是不循环的 那么没有相同的  若是循环的 最小循环节为len 那么共有len 组   i 就属于 i%len这一组

       KMP判断是否循环 且找出最小循环节长度  随便 保存一下 也可以哈希。。。

     1 #include <bits/stdc++.h>
     2 #define pb push_back
     3 #define mp make_pair
     4 #define fi first
     5 #define se second
     6 #define all(a) (a).begin(), (a).end()
     7 #define fillchar(a, x) memset(a, x, sizeof(a))
     8 #define huan printf("
    ");
     9 #define debug(a,b) cout<<a<<" "<<b<<" ";
    10 using namespace std;
    11 typedef long long ll;
    12 const int maxn=1e6+10,inf=0x3f3f3f3f;
    13 const ll mod=1e9+7;
    14 char t[maxn];
    15 int _next[maxn];
    16 int tlen,slen;
    17 void getnext()
    18 {
    19     int j,k;
    20     j=0,k=-1,_next[0]=-1;
    21     while(j<=tlen)
    22         if(k==-1||t[j]==t[k])
    23             _next[++j]=++k;
    24         else
    25             k=_next[k];
    26 }
    27 int main()
    28 {
    29     while(scanf("%s",t)!=EOF)
    30     {
    31         tlen=strlen(t);
    32         set<int> s[tlen+10];
    33         getnext();
    34         if(tlen%(tlen-_next[tlen])==0)
    35         {
    36             int len=tlen-_next[tlen];
    37             for(int i=0;i<tlen;i++)
    38                 s[i%len].insert(i);
    39             printf("%d
    ",len);
    40             for(int i=0;i<len;i++)
    41             {
    42                 printf("%d",s[i].size());
    43                 for(auto it=s[i].begin();it!=s[i].end();it++)
    44                 {
    45                     printf(" %d",*it);
    46                 }
    47                 huan;
    48              }
    49         }
    50         else
    51         {
    52             printf("%d
    ",tlen);
    53             for(int i=0;i<tlen;i++)
    54                 printf("%d %d
    ",1,i);
    55         }
    56     }
    57 }
  • 相关阅读:
    面试中遇到递归算法题别慌--常见递归算法题的解题思路
    Xml日志记录文件最优方案(附源代码)
    linux下源码安装软件
    文本比较算法Ⅴ——回顾贴,对前面几篇文章的回顾与质疑
    从内存中直接运行PE程序
    谈谈Linux内核驱动的coding style
    【全面解禁!真正的Expression Blend实战开发技巧】第六章 认识ListBox
    玩转C链表
    麻省理工《C内存管理和C++面向对象编程》笔记---第一讲:认识C和内存管理
    Dll注入技术之输入法注入
  • 原文地址:https://www.cnblogs.com/stranger-/p/9397210.html
Copyright © 2011-2022 走看看