HDU-3374
String Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2057 Accepted Submission(s): 897
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.
1 1 6 1
1 6 1 6
1 3 2 3
/**
题意:给一个串,然后求串的最小表示法和最大表示法,并且输出有几个
做法:串的最小表示法 + KMP
**/
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string.h>
#include <stdio.h>
#include <queue>
#include <map>
#define maxn 1000005
using namespace std;
int mmin, mmax;
int len;
char t1[maxn];
char str[maxn << ];
char t2[maxn];
void kmp_pre(char x[], int m, int nxt[])
{
int i, j;
j = nxt[] = -;
i = ;
while(i < m)
{
while(- != j && x[i] != x[j]) {
j = nxt[j];
}
nxt[++i] = ++ j;
}
}
int nxt[maxn];
int kmp_count(char x[], int m, char y[], int n)
{
int i, j;
int ans = ;
kmp_pre(x, m, nxt);
i = j = ;
while(i < n)
{
while(- != j && y[i] != x[j]) {
j = nxt[j];
}
i++;
j++;
while(j >= m)
{
ans ++;
j = nxt[j];
}
}
return ans;
}
void matchmin(char s[])
{
int i, j, k, l;
int N = len;
for(i = , j = ; j < N;)
{
for(k = ; k < N && s[i + k] == s[j + k]; k++);
if(k >= N) {
break;
}
if(s[i + k] < s[j + k]) {
j += k + ;
}
else
{
l = i + k;
i = j;
j = max(l, j) + ;
}
}
mmin = i + ;
}
void matchmax(char s[])
{
int i, j, k, l;
int N = len;
for(i = , j = ; j < N;)
{
for(k = ; k < N && s[i + k] == s[j + k]; k++);
if(k >= N) {
break;
}
if(s[i + k] > s[j + k]) {
j += k + ;
}
else
{
l = i + k;
i = j;
j = max(l, j) + ;
}
}
mmax = i + ;
}
int main()
{
while(~scanf("%s", str))
{
int n;
string tmp;
len = strlen(str);
for(int i = ; i < len; i++)
{
str[i + len] = str[i];
}
matchmin(str);
matchmax(str);
for(int i = ; i < len; i++)
{
t1[i] = str[i + mmin - ];
t2[i] = str[i + mmax - ];
}
int res1 = kmp_count(t1, len, str, len + len);
int res2 = kmp_count(t2, len, str, len + len);
if(mmin == ) {
res1 --;
}
if(mmax == ) {
res2 --;
}
printf("%d %d %d %d\n", mmin, res1, mmax, res2);
}
return ;
}
HDU-3374的更多相关文章
- HDU 3374 String Problem(KMP+最大/最小表示)
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- HDU 3374 String Problem (KMP+最大最小表示)
HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others) Memory ...
- HDU - 3374 String Problem (kmp求循环节+最大最小表示法)
做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...
- HDU 3374 最小/大表示法+KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给定一个串s,该串有strlen(s)个循环同构串,要求输出字典序最小的同构串的下标,字典 ...
- HDU 3374 String Problem (KMP+最小最大表示)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3374 [题目大意] 给出一个字符串,求出最小和最大表示是从哪一位开始的,并且输出数量. [题解] ...
- HDU 3374 String Problem(KMP+最大(最小)表示)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:给出一个字符串,依次左移一个单位形成一堆字符串,求其字典序最小和最大的字符串需要左移多 ...
- hdu 3374 String Problem (kmp+最大最小表示法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:输出最大和最小的是从哪一位开始的,同时输出最小循环节的个数. 这里简单介绍对字符串最小 ...
- hdu 3374 String Problem(最小表示法+最大表示法+kmp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给出一个字符串问这个字符串最小表示的最小位置在哪,还有有几个最小表示的串.最大表示的位置在 ...
- hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)
Problem - 3374 KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html 循环节推导的证明相当 ...
- HDU 3374 String Problem (KMP+最大最小表示)
KMP,在有循环节的前提下: 循环节 t = len-next[len], 个数num = len/(len-next[len]);个人理解,如果有循环节,循环节长度必定小于等于len/2, 换句话说 ...
随机推荐
- html5实现web app摇一摇换歌
微信可以摇歌曲,根据声音识别出歌曲,然后返回歌曲信息,利用html5的deviceOrientation特性和deviceMotion事件也可以在web app上实现类似于微信摇一摇的功能,原生的ap ...
- springmvc项目搭建五-postgresql+easyui的数据显示
上一篇虽然完成了页面的显示,但是是假数据,本篇添加了postgresql的数据库,将登陆的校验和数据的显示都通过数据库来完成. 我是在本地搭建了一个postgre的数据库,就先新建两张表吧,一个用于用 ...
- PokeCats开发者日志(八)
现在是PokeCats游戏开发的第十四天的中午,很不幸著作权申请又被打回来了. 据说是排版后代码行数还差500行,文档不足十版.我擦,原来他们会自己排版的啊. 只好从项目自带的xml里扣代 ...
- PhoneGap & HTML5 学习资料网址
PhoneGap 与 Application Cache应用缓存 http://www.html5cn.org/forum.php?mod=viewthread&tid=40272 加速We ...
- (转)Loadrunner监控Linux的17个指标
1.Average load:Average number of processes simultaneously in Ready state during the last minute. 上 ...
- 不错的PDF开发库
C++库: 1,PDF类库 PoDoFo http://podofo.sourceforge.net/ PoDoFo 是一个用来操作 PDF 文件格式的 C++ 类库.它还包含一些小工具用来解析 ...
- (转)java中equals和等号(==)的区别浅谈
java中的数据类型,可分为两类:1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==) ...
- [Leetcode] 3sum-closest 给定值,最为相近的3数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- BZOJ5340 [Ctsc2018]假面 【概率dp】
题目链接 BZOJ5340 题解 我们能很容易维护每个人当前各种血量的概率 设\(p[u][i]\)表示\(u\)号人血量为\(i\)的概率 每次攻击的时候,讨论一下击中不击中即可转移 是\(O(Qm ...
- 怎么给word加底纹