String Problem
HDU - 3374
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.
abcder aaaaaa ababab
1 1 6 1 1 6 1 6 1 3 2 3
关于字符串最小最大表示法讲解:点击打开链接
模板:点击打开链接
code:
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 1000005
using namespace std;
char s[N];
int Next[N];
void getNext(){
int i = -1,j = 0;
memset(Next,0,sizeof(Next));
Next[0] = -1;
int len = strlen(s);
while(j < len){
if(i == -1 || s[i] == s[j]){
i++,j++;
Next[j] = i;
}
else{
i = Next[i];
}
}
}
int min_max_express(bool flag){
int i = 0,j = 1, k = 0;
int len = strlen(s);
while(i < len && j < len && k < len){
int t = s[(j+k)%len]-s[(i+k)%len];
if(t == 0)
k++;
else{
if(flag){
if(t > 0) j = j+k+1;
else i = i+k+1;
}
else{
if(t > 0) i = i+k+1;
else j = j+k+1;
}
if(i == j) j++;
k = 0;
}
}
int ans = min(i,j);
return ans;
}
int main(){
while(~scanf("%s",s)){
getNext();
int mins = min_max_express(true);
int maxs = min_max_express(false);
int len = strlen(s);
int tmp = len-Next[len];
int times = len % tmp ? 1 : len/tmp;
printf("%d %d %d %d\n",mins+1,times,maxs+1,times);
}
return 0;
}
本文介绍了一个经典的字符串问题——寻找一个给定字符串通过左移产生的字典序最小和最大字符串及其出现次数。文章提供了完整的解析过程及代码实现,帮助读者理解字符串处理算法。
1419

被折叠的 条评论
为什么被折叠?



