hdu3374 String Problem KMP+最大最小表示法
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.
题意:给出一个字符串,求出它循环同构的字符串中字典序最小的一个串的开始位置,以及有多少串是同样字典序最小的,然后同样求出字典序最大的串的这两个值
先用最大/最小表示法求出字典序最大或最小的串,并在原串的倍增串中进行KMP匹配。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; const int maxn=1e6+;
char s[maxn<<],ss[maxn<<];
char tmp[maxn];
int p[maxn<<]; inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;} int KMP(char s[],char t[]){
int i,j,ans=; //ans记录字符串出现次数
int n=strlen(s),m=strlen(t); //在题目中遇到过,其实strlen很慢,所以如果不先存起来可能有TLE的风险
p[]=p[]=; //初始化自匹配数组
for(i=;i<m;i++){ //自匹配
j=p[i];
while(j&&t[i]!=t[j])j=p[j];
p[i+]=t[i]==t[j]?j+:;
}
j=; //注意 j=0
for(i=;i<n-;i++){ //串匹配
while(j&&s[i]!=t[j])j=p[j];
if(s[i]==t[j])j++;
if(j==m){
ans++; //此处记录出现次数(模板串在待匹配串中可重叠),或改为直接break表示是否出现过
}
}
return ans;
} int MINR(char s[],int l){
for(int i=;i<l;++i)s[l+i]=s[i];
s[*l]=;
int i=,j=;
while(i<l&&j<l){
int k=;
while(s[i+k]==s[j+k]&&k<l)++k;
if(k==l)return min(i,j);
if(s[i+k]>s[j+k])i=max(i+k+,j+);
else j=max(j+k+,i+);
}
return min(i,j);
} int MAXR(char s[],int l){
for(int i=;i<l;++i)s[l+i]=s[i];
s[*l]=;
int i=,j=;
while(i<l&&j<l){
int k=;
while(s[i+k]==s[j+k]&&k<l)++k;
if(k==l)return min(i,j);
if(s[i+k]<s[j+k])i=max(i+k+,j+);
else j=max(j+k+,i+);
}
return min(i,j);
} int main(){
while(scanf("%s",s)!=EOF){
int l=strlen(s);
strcpy(ss,s);
int pos=MINR(ss,l);
for(int i=;i<l;++i)tmp[i]=ss[i+pos];
tmp[l]=;
printf("%d %d ",pos+,KMP(ss,tmp));
strcpy(ss,s);
pos=MAXR(ss,l);
for(int i=;i<l;++i)tmp[i]=ss[i+pos];
tmp[l]=;
printf("%d %d\n",pos+,KMP(ss,tmp));
}
return ;
}
hdu3374 String Problem KMP+最大最小表示法的更多相关文章
- O - String Problem KMP 字符串最小表示法
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- String Problem hdu 3374 最小表示法加KMP的next数组
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 3374 String Problem (KMP+最大最小表示)
KMP,在有循环节的前提下: 循环节 t = len-next[len], 个数num = len/(len-next[len]);个人理解,如果有循环节,循环节长度必定小于等于len/2, 换句话说 ...
- HDU 3374 String Problem(KMP+最大(最小)表示)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:给出一个字符串,依次左移一个单位形成一堆字符串,求其字典序最小和最大的字符串需要左移多 ...
- HDU3374 String Problem —— 最小最大表示法 + 循环节
题目链接:https://vjudge.net/problem/HDU-3374 String Problem Time Limit: 2000/1000 MS (Java/Others) Me ...
- bzoj5130 字符串的周期(kmp,最小表示法)
bzoj5130 字符串的周期(kmp,最小表示法) bzoj 题解时间 m很大,n很小. 周期很容易求,就是kmp之后n-fail[n]. 之后对于枚举所有的字符串用最小表示法,暴力搜索. 能过就完 ...
- hdu 3374 String Problem(kmp+最小表示法)
Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...
- hdu3374 String Problem【最小表示法】【exKMP】
String Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu3374 String Problem
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目: String Problem Time Limit: 2000/1000 MS (Java/ ...
随机推荐
- bzoj3879
题解: 后缀数组 然后把读入的内容去重,按照rank排序 然后用单调栈处理一下 代码: #include<bits/stdc++.h> using namespace std; typed ...
- selenium(七)expected_conditions EC
判断一个元素是否存在,如何判断alert弹窗出来了,如何判断动态的元素等等一系列的判断,在selenium的expected_conditions模块收集了一系列的场景判断方法, 一.功能介绍和翻译 ...
- 关于Xcode9 无法读取文件的问题
以前我们加载本地文件的时候也许没有注意,可是在Xcode9中会出现许多问题,经常会出现图片无法显示,本地html无法加载等问题: 当然不是Xcode的问题,只是以前我们并没有注意,其实Xcode对这些 ...
- Cracking The Coding Interview 4.4
//Given a binary search tree, design an algorithm which creates a linked list of all the nodes at ea ...
- java动手动脑2
仔细阅读示例: EnumTest.java,运行它,分析运行结果? 你能得到什么结论?你掌握了枚举类型的基本用法了吗? 运行结果: 第一个false是判断s和t是否引用同一个对象,第二个false是判 ...
- oracel 日期查询
--查出当前系统时间select SYSDATE from table; --格式转换-- TO_CHAR 把日期或数字转换为字符串 -- TO_CHAR(number, '格式') - ...
- 自定义String
// ShStringNew.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #inclu ...
- jaxb 组装及解析xml
参考 http://blog.csdn.net/yanan_seachange/article/details/7325708 a.添加依赖 b.建立绑定关系 c.测试 a.添加依赖 <depe ...
- office web apps server安装部署
操作系统:windows 2012 软件下载地址: 链接:https://pan.baidu.com/s/1c3WWFs8 密码:4dcy NDP452-KB2901954-Web.exe(.Net ...
- win7 + nginx + php
1. 下载 Nginx的下载地址:http://www.nginx.org/ PHP的下载地址:http://www.php.NET/downloads.php win7 64 + php-5.4 ...