hdu3374 String Problem【最小表示法】【exKMP】
String Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4828 Accepted Submission(s): 1949
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.
aaaaaa
ababab
1 6 1 6
1 3 2 3
题意:
找到给定的字符串$S$的最小表示法和最大表示法。并且找到他在所有循环同构串中的出现次数。
思路:
首先肯定是要先分别找出最大表示法和最小表示法对应的起始下标。
方法就是先复制一倍接在后面,然后用两个指针分别从0和1开始往后扫描,在第一个发现不相等的位置比较$s[i+k]$和$s[j+k]$的大小。
然后用exKMP分别对最小表示法和最大表示法求$extend$数组。
然后数一下有多少个$extend[i] = n$,这就是个数。
#include<iostream>
#include<bits/stdc++.h>
#include<cstdio>
#include<cmath>
//#include<cstdlib>
#include<cstring>
#include<algorithm>
//#include<queue>
#include<vector>
//#include<set>
//#include<climits>
//#include<map>
using namespace std;
typedef long long LL;
#define N 100010
#define pi 3.1415926535
#define inf 0x3f3f3f3f const int maxn = 1e6 + ;
char s[maxn * ], t[maxn];
int nxt[maxn * ], ex_min[maxn * ], ex_max[maxn * ]; void GETNEXT(char *str)
{
int i=,j,po,len=strlen(str);
nxt[]=len;//初始化next[0]
while(str[i]==str[i+]&&i+<len)//计算next[1]
i++;
nxt[]=i;
po=;//初始化po的位置
for(i=;i<len;i++)
{
if(nxt[i-po]+i<nxt[po]+po)//第一种情况,可以直接得到next[i]的值
nxt[i]=nxt[i-po];
else//第二种情况,要继续匹配才能得到next[i]的值
{
j=nxt[po]+po-i;
if(j<)j=;//如果i>po+next[po],则要从头开始匹配
while(i+j<len&&str[j]==str[j+i])//计算next[i]
j++;
nxt[i]=j;
po=i;//更新po的位置
}
}
}
//计算extend数组
void EXKMP(char *s1,char *s2, int *ex)
{
int i=,j,po,len=strlen(s1),l2=strlen(s2);
GETNEXT(s2);//计算子串的next数组
while(s1[i]==s2[i]&&i<l2&&i<len)//计算ex[0]
i++;
ex[]=i;
po=;//初始化po的位置
for(i=;i<len;i++)
{
if(nxt[i-po]+i<ex[po]+po)//第一种情况,直接可以得到ex[i]的值
ex[i]=nxt[i-po];
else//第二种情况,要继续匹配才能得到ex[i]的值
{
j=ex[po]+po-i;
if(j<)j=;//如果i>ex[po]+po则要从头开始匹配
while(i+j<len&&j<l2&&s1[j+i]==s2[j])//计算ex[i]
j++;
ex[i]=j;
po=i;//更新po的位置
}
}
} int main()
{
while(scanf("%s", s) != EOF){
int n = strlen(s);
for(int i = ; i < n; i++)s[n + i] = s[i];
int i = , j = , k;
while(i < n && j < n){
for(k = ; k < n && s[i + k] == s[j + k]; k++);
if(k == n)break;
if(s[i + k] > s[j + k]){
i = i + k + ;
if(i == j)i++;
}
else{
j = j + k + ;
if(i == j)j++;
}
}
int rnk_min = min(i, j); i = , j = ;
while(i < n && 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 = j + k + ;
if(i == j)j++;
}
else{
i = i + k + ;
if(i == j)i++;
}
}
int rnk_max = min(i, j); int min_cnt = ;
memcpy(t, s + rnk_min, n);
EXKMP(s, t, ex_min);
for(int i = ;i < n; i++){
if(ex_min[i] == n)min_cnt++;
}
int max_cnt = ;
memcpy(t, s + rnk_max, n);
EXKMP(s, t, ex_max);
for(int i = ; i < n; i++){
if(ex_max[i] == n)max_cnt++;
}
printf("%d %d %d %d\n", rnk_min + , min_cnt, rnk_max + , max_cnt);
}
return ;
}
hdu3374 String Problem【最小表示法】【exKMP】的更多相关文章
- hdu String Problem(最小表示法入门题)
hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...
- HDU3374 String Problem —— 最小最大表示法 + 循环节
题目链接:https://vjudge.net/problem/HDU-3374 String Problem Time Limit: 2000/1000 MS (Java/Others) Me ...
- hdu3374 String Problem 最小最大表示法 最小循环节出现次数
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...
- HDU - 3374:String Problem (最小表示法模板题)
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- hdu3374 String Problem KMP+最大最小表示法
Give you a string with length N, you can generate N strings by left shifts. For example let consider ...
- HDU-3374-String Problem(最小表示法, KMP)
链接: https://vjudge.net/problem/HDU-3374 题意: Give you a string with length N, you can generate N stri ...
- hdu3374 String Problem
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目: String Problem Time Limit: 2000/1000 MS (Java/ ...
- HDU3374 字符串最大最小表示法模板
一开始没太看懂什么意思,拿笔反复推了一遍才大概知道最大最小表示法是怎么求的,感觉太神奇了... #include <iostream> #include <cstdio> #i ...
- 2019牛客暑期多校训练营(第七场)A.String【最小表示法】
传送门:https://ac.nowcoder.com/acm/contest/887/A 题意:大意就是给你一个只含有0和1的字符串,找出一种分割方法,使得每个分割出的字符串都是在该字符串自循环节中 ...
随机推荐
- Spark LDA实战
选取了10个文档,其中4个来自于一篇论文,3篇来自于一篇新闻,3篇来自于另一篇新闻. 首先在pom文件中加入mysql-connector-java: <dependency> <g ...
- tmux 共享窗口大小
http://www.cnblogs.com/bamanzi/p/tmux-share-windows-between-sessions.html
- array2json() - Convert PHP arrays to JSON
array2json is a PHP function that will convert the array given as its argument into a JSON string. T ...
- 跟我学SharePoint2013视频培训课程——版本控制示例(15)
课程简介 第15天,SharePoint 2013版本控制示例 视频 SharePoint 2013 交流群 41032413
- 推荐几个Windows工具软件: HDDB - Everything的替代品
主页: http://hddb.xp-zed.com/index.html 简单地说,HDDB 是NTFS磁盘的文件搜索工具,它最开始是模仿 Everything 的,但解决了 Everything ...
- [转]RabbitMQ的安装与客户端的简单实用
原文地址:http://www.cnblogs.com/yangh965/p/5862347.html 本文主要内容是RabbitMQ的安装步骤[Windows系统与linux上的安装]及客户端的简单 ...
- supervisor开机自动启动脚本+redis+MySQL+tomcat+nginx进程自动重启配置
[root@mongodb-host supervisord]# cat mongo.conf [program:mongo]command=/usr/local/mongodb/bin/mongod ...
- 【Matplotlib】利用Python进行绘图
[Matplotlib] 教程:https://morvanzhou.github.io/tutorials/data-manipulation/plt/ 官方文档:https://matplotli ...
- Linux下搭建LAMP环境(YUM)
安装Apache 1.安装Apache yum -y install httpd 2. Apache配置httpd.conf 通过命令 find / -name httpd.conf 找到Apache ...
- (原)关于i++和++i的小程序测试
今天看到一个程序,于是用vs运行了一下,结果出乎我的意料: 代码: ; +(i++))+(+(i++)); i=; +(++i))+(+(++i)); i=; printf("x=%d,y= ...