String Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4828    Accepted Submission(s): 1949

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
 
Author
WhereIsHeroFrom
 
Source

题意:

找到给定的字符串$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】的更多相关文章

  1. hdu String Problem(最小表示法入门题)

    hdu 3374 String Problem 最小表示法 view code#include <iostream> #include <cstdio> #include &l ...

  2. HDU3374 String Problem —— 最小最大表示法 + 循环节

    题目链接:https://vjudge.net/problem/HDU-3374 String Problem Time Limit: 2000/1000 MS (Java/Others)    Me ...

  3. hdu3374 String Problem 最小最大表示法 最小循环节出现次数

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; int ...

  4. HDU - 3374:String Problem (最小表示法模板题)

    Give you a string with length N, you can generate N strings by left shifts. For example let consider ...

  5. hdu3374 String Problem KMP+最大最小表示法

    Give you a string with length N, you can generate N strings by left shifts. For example let consider ...

  6. HDU-3374-String Problem(最小表示法, KMP)

    链接: https://vjudge.net/problem/HDU-3374 题意: Give you a string with length N, you can generate N stri ...

  7. hdu3374 String Problem

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目: String Problem Time Limit: 2000/1000 MS (Java/ ...

  8. HDU3374 字符串最大最小表示法模板

    一开始没太看懂什么意思,拿笔反复推了一遍才大概知道最大最小表示法是怎么求的,感觉太神奇了... #include <iostream> #include <cstdio> #i ...

  9. 2019牛客暑期多校训练营(第七场)A.String【最小表示法】

    传送门:https://ac.nowcoder.com/acm/contest/887/A 题意:大意就是给你一个只含有0和1的字符串,找出一种分割方法,使得每个分割出的字符串都是在该字符串自循环节中 ...

随机推荐

  1. Nginx配置静态资源

    静态服务器 静态服务器概念非常简单:当用户请求静态资源时,把文件内容回复给用户. 但是,要把静态服务做到极致,需要考虑的方面非常多: 正确书写header:设置content-type.过期时间等 效 ...

  2. 【Windows】Windows中解析DOS的for命令使用

    目录结构: contents structure [+] 简介 for /d ... in ... 案例 案例:打印C://根目录下所有的文件夹名称 案例:打印当前路径下,只有1-3个字母的文件夹名 ...

  3. Swift Precondition 预处理

    前言 precondition 和 assert 的格式类似,也是动态的,precondition 会造成程序的提前终止并抛出错误信息. 1.Precondition precondition 在一般 ...

  4. intellij IDEA 安装和配置和使用

    下载:https://www.jetbrains.com/idea/download/download-thanks.html?platform=windows 安装教程:https://blog.c ...

  5. 空间谱专题13:联合解算DOA(ML/AP)

    其中作者:桂. 时间:2017-10-16  07:51:40 链接:http://www.cnblogs.com/xingshansi/p/7675380.html 前言 主要记录二维测向中,分别利 ...

  6. 《网络编程》IPv4 与 IPv6 相互操作

    前言 因为互联网终端不断添加.IPv4 地址长度(32位)已不可以满足要求.所以出现了 IPv6地址(128位).可是现有应用程序大部分还是採用 IPv4 地址形式,所以必须解决 IPv4 与 IPv ...

  7. pythn os

    获取文件所在路径 import os os.path.dirname(__file__)  获取当前文件的所在路径 print (os.path.dirname(os.path.dirname(__f ...

  8. 【Java】分布式RPC通信框架Apache Thrift 使用总结

    简介 Apache Thrift是Facebook开源的跨语言的RPC通信框架,目前已经捐献给Apache基金会管理,由于其跨语言特性和出色的性能,在很多互联网公司得到应用,有能力的公司甚至会基于th ...

  9. js判断登陆用户名及密码是否为空的简单实例

    js判断登陆用户名及密码是否为空的简单实例 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 <script type="text/javascript ...

  10. kafka性能测试1.0.0

    kafka提供工具kafka-producer-perf-test.sh用以压测, 参数 说明 messages 生产者发送总的消息数量 message-size 每条消息大小 batch-size ...