http://acm.hdu.edu.cn/showproblem.php?pid=3374

String Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2045    Accepted Submission(s): 891

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
 
输出:最小字典序的编号,最小字典序个数,最大字典序编号,最大字典序个数。

刚开始一直没明白是怎么回事,终于明白了吧,用暴力写了,明明知道时间超限,但我还是交了一次,果断的TLE,后来搜了搜,在看了许久后终于明白是怎么一回事,原来是用了两个指针, 用两个指针来动态比较,然后的然后就能求出最小字典串和最大字典串了,另外个数的问题其实只需要FindNext就可以求出来了,这个刚开始的时候是一直没想到,我只是想暴力求出来,然后发现自己忽略了这么好用的。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm> using namespace std; const int maxn = ; int Next[maxn], sum;
char s1[maxn], s[maxn]; void FindNext(char S[])
{
int i=, j=-;
Next[] = -; int Slen = strlen(S); while(i<Slen)
{
if(j==- || S[i]==S[j])
Next[++i] = ++j;
else j = Next[j];
}
} int GetMin(char s[], int N)
{
int i=, j=; while(i<N && j<N)
{
int k=;
while(s[i+k]==s[j+k] && k<N) k++;
if(k==N)
break; if(s[i+k]<s[j+k])
{
if(j+k>i)
j += k+;
else
j = i+;
}
else
{
if(i+k>j)
i += k+;
else
i = j+;
}
} return min(i, j);
} int GetMax(char s[], int N)
{
int i=, j=; while(i<N && j<N)
{
int k=;
while(s[i+k]==s[j+k] && k<N) k++;
if(k==N)
break; if(s[i+k]>s[j+k])
{
if(j+k>i)
j += k+;
else
j = i+;
}
else
{
if(i+k>j)
i += k+;
else
i = j+;
}
} return min(i, j);
} int main()
{
while(scanf("%s", s1)!=EOF)
{
int N = strlen(s1); strcpy(s, s1);
strcat(s, s1); FindNext(s);
int circle = N - Next[N], time=; if(N%circle==)
time = N / circle; printf("%d %d %d %d\n", GetMin(s, N)+, time, GetMax(s, N)+, time);
} return ;
}

(KMP 最大表示最小表示)String Problem -- hdu-- 3374的更多相关文章

  1. String Problem hdu 3374 最小表示法加KMP的next数组

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. String Problem - HDU 3374 (kmp+最大最小表示)

    题目大意:有一个字符串长度为N的字符串,这个字符串可以扩展出N个字符串,并且按照顺序编号,比如串  ” SKYLONG “ SKYLONG 1 KYLONGS 2 YLONGSK 3 LONGSKY ...

  3. String Problem HDU - 3374(最大最小表示法+循环节)

    题意: 给出一个字符串,问这个字符串经过移动后的字典序最小的字符串的首字符位置和字典序最大的字符串的首字符的位置,和能出现多少次最小字典序的字符串和最大字典序的字符串 解析: 能出现多少次就是求整个字 ...

  4. HDU 3374 String Problem(KMP+最大/最小表示)

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  5. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  6. HDU - 3374 String Problem (kmp求循环节+最大最小表示法)

    做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...

  7. hdu String Problem(最小表示法入门题)

    hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...

  8. 【HDU3374】 String Problem (最小最大表示法+KMP)

    String Problem Description Give you a string with length N, you can generate N strings by left shift ...

  9. HDOJ3374 String Problem 【KMP】+【最小表示法】

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

随机推荐

  1. Shiro权限总结

    参考学习地址   shiro 瞅完就会用(ssm+shiro)    Spring Shiro配置实现用户认证和授权 anon:它对应的过滤器里面是空的,什么都没做,另外.do和.jsp后面的*表示参 ...

  2. 发布MVC项目到服务器上时候遇到的 模块 DirectoryListingModule 通知 ExecuteRequestHandler 处理程序 StaticFile 错误代码 0x00000000

    应用程序“HMW121197”中的服务器错误错误摘要HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容. 详细错误信息模块 DirectoryListingM ...

  3. SpringBoot整合Shiro (二)

    Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码学和会话管理.相比较Spring Security,shiro有小巧.简单.易上手等的优点.所以很多框架都在使用sh ...

  4. oracle存储过程-获取错误信息

    dbms_output.put_line('code:' || sqlcode); dbms_output.put_line('errm:' || sqlerrm); dbms_output.put_ ...

  5. 奇偶数判断2(if else+switch语句)

    public class 奇偶数判断2 { public static void main(String [] agrs){ float s = 17f; //定义浮点型数据s float h = s ...

  6. Javascript读写CSS属性

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. PAT L3-004 肿瘤诊断(三维广搜)

    在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是每张切片的尺寸(即每张切片 ...

  8. git pull和git fetch命令

    git pull和git fetch命令 git pull git pull命令的作用是取回远程主机某个分支的更新,在与本地指定分支合并,格式如下: $ git pull <远程主机名>& ...

  9. loadrunner--常用函数列表【转】

    1.        Intweb_reg_save_param("参数名","LB=左边界","RB=右边界",LAST);/注册函数,在参 ...

  10. 我所理解的 Laravel 请求 生命周期

    转载自:https://laravel-china.org/topics/3343/my-understanding-of-the-laravel-request-life-cycle 当你使用一个工 ...