HDU 3374String Problem(最大最小表示法+KMP)
来源:互联网 发布:手机怎么注册淘宝店铺 编辑:程序博客网 时间:2024/04/30 01:43
String Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1292 Accepted Submission(s): 581
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.
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
abcderaaaaaaababab
Sample Output
1 1 6 11 6 1 61 3 2 3
题目大意:循环移位,一直移到尾变成头,中间的出现的情况,在里面找字典序最前的和字典序最后的下标标号,然后把次数也输出来。
解题思路:很容易想到次数肯定是用最小循环节求出来,然后就是如何找出字典序最小的,一直在纠结这个地方。开始将最大最小表示法神化了,当时比赛碰到那个题多校联赛2012第四场的C题,也不敢下手。其实最大最小表示法原来就是暴力。。。而我优化了一点,是因为在找字典序最小最大的时候,只需要找编号在最小循环节以前的即可,代码中的len1.详解见代码。
题目地址:String Problem
AC代码:
#include<iostream>#include<string>#include<cstring>#include<cstdio>#define MAX 1000005using namespace std;char p[MAX];char p1[MAX*2]; //把模式串复制一份int len,next[MAX],cnt,len1;void getnext(){ int i,j; next[0]=0,next[1]=0; for(i=1;i<len;i++) { j=next[i]; while(j&&p[i]!=p[j]) j=next[j]; if(p[i]==p[j]) next[i+1]=j+1; else next[i+1]=0; }}int getmin() //最小表示法{ int i=0,j=1,k=0; while(i<len1&&j<len1&&k<len1) { if(p1[i+k]==p1[j+k]) k++; else if(p1[i+k]<p1[j+k]) { j=j+k+1; k=0; } else { i=i+k+1; k=0; } if(i==j) j++; } return i;}int getmax() //最大表示法{ int i=0,j=1,k=0; while(i<len1&&j<len1&&k<len1) { if(p1[i+k]==p1[j+k]) k++; else if(p1[i+k]<p1[j+k]) { i=i+k+1; k=0; } else { j=j+k+1; k=0; } if(i==j) j++; } return i;}int main(){ while(~scanf("%s",p)) { len=strlen(p); strcpy(p1,p); strcat(p1,p); getnext(); cnt=1; //记录循环的次数,利用最小循环节的知识 if(len%(len-next[len])==0) cnt=len/(len-next[len]); len1=len/cnt; //只考虑循环节以前的即可 printf("%d %d %d %d\n",getmin()+1,cnt,getmax()+1,cnt); } return 0;}
- HDU 3374String Problem(最大最小表示法+KMP)
- hdu 3374 String Problem (kmp+最大最小表示法)
- HDU 3374 String Problem (KMP+最大最小表示法)
- HDU 3374 String Problem(最大最小表示法+kmp)
- hdu 3374 String Problem(KMP+最大最小表示法)
- HDU 3374 String Problem(最小最大表示法+KMP)
- HDU 3374 String Problem KMP-最小最大表示法
- hdu 3374 String Problem(字符串最小最大表示法+kmp)
- HDU 3374 String Problem (KMP+最大最小表示)
- HDU 3374 String Problem (KMP+最大最小表示)
- HDU 3374 String Problem (KMP+最大最小表示)
- HDU 3374 String Problem (KMP+最小最大表示法)【模板】
- HDU 3374 String Problem(最大最小表示法模板+KMP+next数组的运用)
- hdu 3374 String Problem(KMP+字符串最小最大表示)
- hdu 3374 String Problem (最小表示法+KMP)
- hdu 3374 String Problem (最小表示法+kmp next[])
- hdu 3374 String Problem 【kmp+最小(大)表示法】
- hdu 3374 String Problem(最小表示法+kmp)
- windows编程之helloworld
- 反素数
- 进程控制之 fork、wait、exec族函数 及进程资源控制
- 二分匹配的Hopcroft-Carp算法
- UltraEdit无法打开
- HDU 3374String Problem(最大最小表示法+KMP)
- 原来java的正则也支持后置引用的,以前一直没有注意到。
- Java通过JNI调用C语言的方法
- POJ2594(二分匹配+Floyd求传递闭包)
- 触发器、索引、存储过程以及函数
- C++数据结构--归并排序
- win7下创建逻辑分区
- Eclipse快捷鍵
- 组合数取模深度解析