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. 关于react-redux中Provider、connect的解析

    Provider 是什么 react-redux 提供的一个 React 组件 作用 把 store 提供给其子组件 //使用 redux 的 createStore 方法创建的一个 store co ...

  2. 给曾经是phper的程序员推荐个学习网站

    如果你原来是一个php程序员,你对于php函数非常了解(PS:站长原来就是一个php程序员),但是现在由于工作或者其他原因要学习python,但是python很多函数我们并不清楚,在这里我给大家推荐一 ...

  3. spring环境搭建(简单实例)

    1使用Maven导入需要的依赖(在project标签下) <properties> <spring_version>3.2.2.RELEASE</spring_versi ...

  4. PAT 1065 单身狗

    https://pintia.cn/problem-sets/994805260223102976/problems/994805266942377984 “单身狗”是中文对于单身人士的一种爱称.本题 ...

  5. Promise使用手册

    导读 Promise问世已久, 其科普类文章亦不计其数. 遂本篇初衷不为科普, 只为能够温故而知新. 比如说, catch能捕获所有的错误吗? 为什么有些时候会抛出"Uncaught (in ...

  6. 【EasyNetQ】- 连接RabbitMQ

    如果您习惯于处理与SQL Server等关系数据库的连接,那么您可能会发现EasyNetQ处理连接的方式有点奇怪.与关系数据库的通信始终由客户端启动.客户端打开连接,发出SQL命令,在必要时处理结果, ...

  7. 页面加载时给的子元素的第一个元素加class

    HTML代码: <div id="xiao"> <ul> <li></li> </ul> </div> js ...

  8. Maven中mirrors和repository的关系

    一.前言 之前没有搞清楚pom.xml里面配置的repositorys节点配置的仓库和mirrors里面配置的仓库的“镜像”间的关系,特意去搜索了相关内容,这边有篇文章讲得透彻,所以这边转载一下~ 二 ...

  9. SQL Server “超过了锁请求超时时段”错误

    错误提示:“已超过了锁请求超时时段. (Microsoft SQL Server,错误: 1222)”(英文:“Lock Request time out period exceeded.(Micro ...

  10. 图解WinXP局域网共享设置步骤

    原文链接地址:http://blog.csdn.net/jackinzhou/article/details/8468208 第一章:共享的前提工作 1.更改不同的计算机名,设置相同的工作组! 2.我 ...