kuangbin专题十六 KMP&&扩展KMP HDU3347 String Problem(最小最大表示法+kmp)

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.OutputOutput 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

只知道最小最大表示法。但是名次没思路。 后来看题解是循环节。。。。脑子真是废了。。  循环节用next数组搞定 即可
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=2000010;
 6 char s[maxn],t[maxn];
 7 int Next[maxn],len;
 8
 9 void prekmp(char* s) {
10     int i,j;
11     j=Next[0]=-1;
12     i=0;
13     while(i<len) {
14         while(j!=-1&&s[i]!=s[j]) j=Next[j];
15         if(s[++i]==s[++j]) Next[i]=Next[j];
16         else Next[i]=j;
17     }
18 }
19
20 int getmin(char* str) {
21     int len2=strlen(str);
22     int i=0,j=1,k=0;
23     while(i<len&&j<len&&k<len) {
24         int tmp=s[(i+k)%len2]-s[(j+k)%len2];
25         if(!tmp) k++;
26         else {
27             if(tmp<0) j+=k+1;
28             else i+=k+1;
29             if(i==j) j++;
30             k=0;
31         }
32     }
33     return min(i,j);
34 }
35
36 int getmax(char* str) {
37     int len2=strlen(str);
38     int i=0,j=1,k=0;
39     while(i<len&&j<len&&k<len) {
40         int tmp=s[(i+k)%len2]-s[(j+k)%len2];
41         if(!tmp) k++;
42         else {
43             if(tmp<0) i+=k+1;
44             else j+=k+1;
45             if(i==j) j++;
46             k=0;
47         }
48     }
49     return min(i,j);
50 }
51
52 int main() {
53     while(~scanf("%s",s)) {
54         len=strlen(s);
55         prekmp(s);
56         int num=1,len1=len-Next[len];
57         if(len%len1==0)
58             num=len/len1;
59         strcpy(t,s);
60         strcat(s,t);
61         printf("%d %d %d %d\n",getmin(s)+1,num,getmax(s)+1,num);
62     }
63 }


原文地址:https://www.cnblogs.com/ACMerszl/p/10321817.html

时间: 2024-10-08 11:32:11

kuangbin专题十六 KMP&&扩展KMP HDU3347 String Problem(最小最大表示法+kmp)的相关文章

kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU1686 Oulipo

The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pair normal, mais tout s’affirmait faux. Tout avait Fair normal, d’abord, pu

kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU1711 Number Sequence

Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1 <= M <= 10000, 1 <= N <= 1000000). Your task is to find a number K which make a[K] = b[1], a[K + 1] = b[2], ...... , a[K + M - 1] = b[M]. If there are

kuangbin专题十六 KMP&amp;&amp;扩展KMP POJ3080 Blue Jeans

The Genographic Project is a research partnership between IBM and The National Geographic Society that is analyzing DNA from hundreds of thousands of contributors to map how the Earth was populated. As an IBM researcher, you have been tasked with wri

kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU1238 Substrings

You are given a number of case-sensitive strings of alphabetic characters, find the largest string X, such that either X, or its inverse can be found as a substring of any of the given strings. InputThe first line of the input file contains a single

kuangbin专题十六 KMP&amp;&amp;扩展KMP HDU3613 Best Reward(前缀和+manacher or ekmp)

After an uphill battle, General Li won a great victory. Now the head of state decide to reward him with honor and treasures for his great exploit. One of these treasures is a necklace made up of 26 different kinds of gemstones, and the length of the

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher :G - Power Strings POJ - 2406(kmp简单循环节)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher G - Power Strings POJ - 2406 题目: Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of

开发指南专题十六:JEECG微云快速开发平台Excel导出

Ladda 应用提交表单的时候显示loading加载中 包括不同位置,不同效果 不同大小,位置,效果,进度条等 演示 XML/HTML Code <article class="examples" style="margin-top:0px;"> <section class="button-demo"> <h3>expand-left</h3> <button class="lad

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher B - Oulipo HDU - 1686(kmp)

B - Oulipo HDU - 1686 题目链接:https://vjudge.net/contest/70325#problem/B 题目: The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A quote from the book: Tout avait Pa

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; ManacherK - Count the string HDU - 3336(前缀数量问题)

K - Count the string HDU - 3336 题目链接:https://vjudge.net/contest/70325#problem/K 题目: It is well known that AekdyCoin is good at string problems as well as number theory problems. When given a string s, we can write down all the non-empty prefixes of t