您的位置:首页 > 其它

String Problem

2015-09-17 19:52 288 查看

String Problem

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
Each line contains one line the string S with length N (N <= 1000000) formed by lower case letters.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

abcder

aaaaaa

ababab

[align=left]Sample Output[/align]

1 1 6 1

1 6 1 6

1 3 2 3

[align=left]Author[/align]
WhereIsHeroFrom

[align=left]Source[/align]
HDOJ Monthly Contest – 2010.04.04
题意:给你一字符串,问最小字典序还有最大字典序的最小下标,以及包含最小字典序和最大字典序的串的开始的不同下标个数。
不知道动不动就TLE什么鬼……

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>

using namespace std;

const int maxn = 1e6+7;

char s[maxn*2], str[maxn];
int Next[maxn], len;

void Getnext(char s[])
{
int j, k;
j = 0;
k = Next[0] = -1;
while(j < len)
{
while(k != -1 && s[j] != s[k])
k = Next[k];
Next[++j] = ++k;
}
}

int Getmax(char s[])   // 最大最小表示法
{
int i, j;
i = 0;
j = 1;
while(i < len && j < len)
{
int k = 0;
while(s[i+k] == s[j+k] && k < len)
k++;
if(k == len)
break;

if(s[i+k] > s[j+k])
{
if(j+k > i)
j = j+k+1;
else
j = i + 1;
}
else
{
if(i+k > j)
i = i + k + 1;
else
i = j + 1;
}
}
return min(i, j);
}

int Getmin(char s[])
{
int i, j;
i = 0;
j = 1;
while(i < len && j < len)
{
int k = 0;
while(s[i+k] == s[j+k] && k < len)
k++;
if(k == len)
break;
if(s[i+k] < s[j+k])
{
if(j+k > i)
j = j+k+1;
else
j = i + 1;
}
else
{
if(i+k > j)
i = i + k + 1;
else
i = j + 1;
}
}
return min(i, j);
}

int main()
{
while(gets(str))
{
len = strlen(str);
strcpy(s, str);
strcat(s, str);

memset(Next, 0, sizeof(Next));

Getnext(str);
int t = 1, k = len - Next[len];   // k 最小循环节的长度,最小循环节,神奇的东西……

if(len % k == 0)
t = len / k;
printf("%d %d %d %d\n", Getmin(s)+1, t, Getmax(s)+1, t);
}
return 0;
}


写代码你就好好写代码………………………………………………………………………………………………
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: