String Problem
2015-09-17 19:52
309 查看
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; }
写代码你就好好写代码………………………………………………………………………………………………
相关文章推荐
- hdu 5444 构建二叉树,搜索二叉树
- 双系统的安装和删除
- Linux 基础知识:Bash的内置变量
- iOS 开发学习 导航控制器(UINavigationController)
- 员工管理系统(文件读写更新版)
- gradle生命周期
- Android 笔记8
- 结构体和联合体
- Mac更新默认Subversion版本
- windows下的Jekyll安装总结
- iOS开发中通过点击按钮异步加载图片
- Ubuntu12.04 安装adb
- Android下使用全局变量
- 多线程
- JS 对象(Object)和字符串(String)互转
- CoreData实践(六)——数据删除
- 第三周项目(5):建设“顺序表”算法库
- ReentrantLock Condition 用法
- 拆分组件名
- CodeForces 12C - Fruits