博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3374 String Problem
阅读量:4640 次
发布时间:2019-06-09

本文共 3247 字,大约阅读时间需要 10 分钟。

String Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1233    Accepted Submission(s): 550

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.
 

 

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
abcder
aaaaaa
ababab
 

 

Sample Output
1 1 6 1
1 6 1 6
1 3 2 3
 解题方法:先用最小表示法和最大表示法求出其开始位置,然后用KMP算法求出循环次数。
#include 
#include
#include
using namespace std;char str[1000005]; int _next[1000005]; int Getmin(char str[]){ int nLen = strlen(str); int i = 0, j = 1, k; while(i < nLen && j < nLen) { for (k = 0; k < nLen; k++) { if (str[(i + k) % nLen] != str[(j + k) % nLen]) { break; } } if (str[(i + k) % nLen] > str[(j + k) % nLen]) { i += k + 1; } else { j += k + 1; } if (i == j) { j++; } } return min(i, j);}int Getmax(char str[]){ int nLen = strlen(str); int i = 0, j = 1, k; while(i < nLen && j < nLen) { for (k = 0; k < nLen; k++) { if (str[(i + k) % nLen] != str[(j + k) % nLen]) { break; } } if (str[(i + k) % nLen] < str[(j + k) % nLen]) { i += k + 1; } else { j += k + 1; } if (j ==i) { j++; } } return min(i, j);}void Getnext(char str[]){ int j = 0, k = -1; int nLen = strlen(str); _next[0] = -1; while(j < nLen) { if (k == -1 || str[k] == str[j]) { j++; k++; _next[j] = k; } else { k = _next[k]; } }}int main(){ while(scanf("%s", str) != EOF) { int n = Getmin(str); int n1 = Getmax(str); Getnext(str); int nLen = strlen(str); int nCount = 0; if (nLen % (nLen - _next[nLen]) == 0) { nCount = nLen / (nLen - _next[nLen]); } else { nCount = 1; } printf("%d %d %d %d\n", n + 1, nCount, n1 + 1, nCount); } return 0;}

 

 

Author
WhereIsHeroFrom
 

转载于:https://www.cnblogs.com/lzmfywz/p/3162833.html

你可能感兴趣的文章
值类型与引用类型
查看>>
This kernel requires an x86-64 CPU, but only detected an i686 CPU.
查看>>
PAT 1023 Have Fun with Numbers[大数乘法][一般]
查看>>
三维空间中的几种坐标系
查看>>
乘法表
查看>>
4.express 框架
查看>>
Java基础算法集50题
查看>>
Android 桌面组件widget
查看>>
25-字符串
查看>>
萌新报道
查看>>
Asp.Net 获取物理路径
查看>>
Apache2.4使用require指令进行访问控制--允许或限制IP访问/通过User-Agent禁止不友好网络爬虫...
查看>>
Solr reRankQuery加自定义函数实现搜索二次排序
查看>>
基于ipv6的抓包实验
查看>>
latex学习(四)tlmgr
查看>>
centos6.5 bugzilla4.4.5 汉化
查看>>
ros topic 发布一次可能会接收不到数据
查看>>
字符串的扩展
查看>>
冒泡排序_c++
查看>>
linux常见术语示意
查看>>