zoukankan      html  css  js  c++  java
  • hdu 3374

    Problem Description
    Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings:
    String Rank 
    SKYLONG 1
    KYLONGS 2
    YLONGSK 3
    LONGSKY 4
    ONGSKYL 5
    NGSKYLO 6
    GSKYLON 7
    and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once.
      Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
     
    Input
      Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.
     
    Output
    Output four integers separated by one space, lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), the string’s times in the N generated strings, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
     
    Sample Input
    abcder aaaaaa ababab
     
    Sample Output
    1 1 6 1 1 6 1 6 1 3 2 3
     
    Author
    WhereIsHeroFrom
     
    Source

     利用nxt数组很好推出循环次数,重点是最小最大表示法。

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <cstdio>
     5 const int N = 1000000 + 11;
     6 using namespace std;
     7 char ss[N],s[N<<1];
     8 int cs,n,a[N<<1],nxt[N];
     9 
    10 void pre()
    11 {
    12     nxt[1] = 0; int k = 0;
    13     for(int i = 2; i <= n; ++i)
    14     {
    15         while(k > 0 && a[i] != a[k+1]) k = nxt[k];
    16         if(a[i] == a[k+1]) ++k;
    17         nxt[i] = k;
    18     }
    19 }
    20 
    21 void Init()
    22 {
    23     n = strlen(ss);
    24     strcpy(s,ss);
    25     strcat(s,ss);
    26     for(int i = 0; i < ( n << 1 ); ++i) a[i+1] = s[i]; 
    27     pre();
    28 }
    29 
    30 int minx()
    31 {
    32     
    33     int i = 1,j = 2,k = 0;
    34     while(i <= n && j <= n  && k < n)
    35     {
    36         if(a[i+k] == a[j+k] ) ++k;
    37         else if(a[i+k] < a[j+k]) j = j + k + 1, k = 0;
    38         else i = i + k + 1, k = 0;
    39         if( i == j ) ++j;
    40     }
    41     return i;
    42 }
    43  
    44 void Solve()
    45 {
    46     cs = n - nxt[n]; 
    47     if(n % cs == 0) cs = n/cs; else cs = 1; n = n/cs;
    48     printf("%d %d ",minx(),cs);
    49     for(int i = 1;i <= ( n << 1 ); ++i) a[i] = 0-a[i];
    50     printf("%d %d",minx(),cs);
    51 }
    52 
    53 
    54 int main()
    55 {
    56     while(~scanf("%s",ss))
    57     {
    58         Init();
    59         Solve();
    60         puts("");
    61     }
    62     return 0;
    63 }
  • 相关阅读:
    【实践】腾讯云服务器发布项目
    【实践】jdbc批量插入数据
    【转载】jQuery Validate 菜鸟教程
    【转载】jQuery Validate验证框架 + CKEditor 无法验证问题的解决方法
    【实践】获取CKEditor的html文本、纯文本、被选中的内容及赋值
    CKEDITOR的内容js转码,C#控制器解码接收
    jQuery.qrcode生成二维码
    一个表中的两列与另一个表的一列关联的结果显示
    【转载】海明码求解公式规律(转载自计算机网络高分笔记)
    『实践』百度地图同时显示多个路书
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6640977.html
Copyright © 2011-2022 走看看