String Problem

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

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
/**
题意:给一个串,然后求串的最小表示法和最大表示法,并且输出有几个
做法:串的最小表示法 + 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的更多相关文章

  1. HDU 3374 String Problem(KMP+最大/最小表示)

    String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  2. HDU 3374 String Problem (KMP+最大最小表示)

    HDU 3374 String Problem (KMP+最大最小表示) String Problem Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  3. HDU - 3374 String Problem (kmp求循环节+最大最小表示法)

    做一个高产的菜鸡 传送门:HDU - 3374 题意:多组输入,给你一个字符串,求它最小和最大表示法出现的位置和次数. 题解:刚刚学会最大最小表示法,amazing.. 次数就是最小循环节循环的次数. ...

  4. HDU 3374 最小/大表示法+KMP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给定一个串s,该串有strlen(s)个循环同构串,要求输出字典序最小的同构串的下标,字典 ...

  5. HDU 3374 String Problem (KMP+最小最大表示)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=3374 [题目大意] 给出一个字符串,求出最小和最大表示是从哪一位开始的,并且输出数量. [题解] ...

  6. HDU 3374 String Problem(KMP+最大(最小)表示)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:给出一个字符串,依次左移一个单位形成一堆字符串,求其字典序最小和最大的字符串需要左移多 ...

  7. hdu 3374 String Problem (kmp+最大最小表示法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题目大意:输出最大和最小的是从哪一位开始的,同时输出最小循环节的个数. 这里简单介绍对字符串最小 ...

  8. hdu 3374 String Problem(最小表示法+最大表示法+kmp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3374 题意:给出一个字符串问这个字符串最小表示的最小位置在哪,还有有几个最小表示的串.最大表示的位置在 ...

  9. hdu 3374 String Problem (字符串最小最大表示 + KMP求循环节)

    Problem - 3374   KMP求循环节. http://www.cnblogs.com/wuyiqi/archive/2012/01/06/2314078.html   循环节推导的证明相当 ...

  10. HDU 3374 String Problem (KMP+最大最小表示)

    KMP,在有循环节的前提下: 循环节 t = len-next[len], 个数num = len/(len-next[len]);个人理解,如果有循环节,循环节长度必定小于等于len/2, 换句话说 ...

随机推荐

  1. tomcat 路径"/"表示根目录

  2. [NOIP2017 TG D1T2]时间复杂度

    题目大意:略 题解:模拟 卡点:1.数组忘清空 (考场代码风格独特...) C++ Code: #include<cstdio> #include<cstring> #incl ...

  3. 【bzoj2038】[国家集训队2010]小Z的袜子 莫队

    莫队:就是一坨软软的有弹性的东西Duang~Duang~Duang~ 为了防止以左端点为第一关键字以右端点为第二关键字使右端点弹来弹去,所以让左端点所在块为关键字得到O(n1.5)的时间效率,至于分块 ...

  4. 一个JavaScript日期格式化扩展函数

    我们都知道在Java和PHP语言中,有专门用于格式化日期对象的类和函数,例如Java中的DateFormat等等,通过这些类和函数,我们可以方便的将一个日期对象按照格式的要求输出为字符串,例如对于同一 ...

  5. Python3 urlparse

    >>> from urllib.parse import urlparse >>> o = urlparse('http://www.cwi.nl:80/%7Egu ...

  6. BZOJ1191:超级英雄(二分图匹配)

    [HNOI2006]超级英雄Hero 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1191 Description: 现在电视台有一种节 ...

  7. TCP ------ TCP创建服务器中出现的套接字

    在服务器端,socket()返回的套接字用于监听(listen)和接受(accept)客户端的连接请求.这个套接字不能用于与客户端之间发送和接收数据. accept()接受一个客户端的连接请求,并返回 ...

  8. React 获取 url 参数 —— this.props.match

    在 react 组件的  componentDidMount 方法中打印一下 this.props,在浏览器控制台中查看输出如下: 其中页面的 url 信息全都包含在 match 字段中,以地址 lo ...

  9. java属性为什么没多态,而是方法多态

    定义 java多肽的特性:方法具有多态性,属性却没有. 准备 基类: 子类: 测试类: 结果: 分析如下 父类 a=new 子类,实际对象时子类.由于向上转型,我们可以用父类在编译期间代替子类,使得编 ...

  10. mysql 基本操作练习

    ), sex ), age )); , '北京'); , '上海'); , '广州'); , '北京'); , '北京'); , '上海'); , '北京'); #(). 写出sql语句,查询所有年龄 ...