String 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


  用最大最小表示法,KMP求个数。
  注意特殊的时候在KMP中加入长度特判。

  最小最大表示法是求出循环子串得到最小最大表示。

  表示我之前并没有遇到过,貌似还有些神奇的应用?不知道、、

  但是打起来还是挺简单的,也很容易证明。

  

int get_st(bool p)
{
int i=1,j=2,k=0;
while(i<=l&&j<=l&&k<=l)
{
if(s[i+k]==s[j+k]) k++;
else if((s[i+k]>s[j+k])==(!p))
{
i+=k+1;
k=0;
}
else
{
j+=k+1;
k=0;
}
if(i==j) j++;
}
return mymin(i,j);
}

  

  这个我把最小表示和最大表示合在一起了。p=0是最小,p=1时最大。

  证明的话...连续匹配过程中一旦发现字符有小于其他字符的,那么他一定不是最大表示,且他前面连续的一段也不是。

本题代码如下:

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 1000010 char s[*Maxn];
int nt[Maxn*],l; int mymin(int x,int y) {return x<y?x:y;} int get_st(bool p)
{
int i=,j=,k=;
while(i<=l&&j<=l&&k<=l)
{
if(s[i+k]==s[j+k]) k++;
else if((s[i+k]>s[j+k])==(!p))
{
i+=k+;
k=;
}
else
{
j+=k+;
k=;
}
if(i==j) j++;
}
return mymin(i,j);
} void get_nt(int x)
{
nt[x]=x-;int p=x-;
for(int i=x+;i<=x+l-;i++)
{
while(s[i]!=s[p+]&&p>=x) p=nt[p];
if(s[i]==s[p+]) p++;
nt[i]=p;
}
} int ffind(int x)
{
int ans=;int p=x-;
for(int i=;i<=*l;i++)
{
while((s[i]!=s[p+]&&p>=x)||p-x+>=l) p=nt[p];
if(s[i]==s[p+]&&p-x+<l) p++;
if(i>l&&p-x+>=l) ans++;
}
return ans;
} int main()
{
while(scanf("%s",s+)!=EOF)
{
l=strlen(s+);
for(int i=;i<=l;i++) s[i+l]=s[i];
int st=get_st();
get_nt(st);
printf("%d %d ",st,ffind(st)); st=get_st();
get_nt(st);
printf("%d %d\n",st,ffind(st));
}
return ;
}

HDU3374

2016-08-17 15:59:59

【HDU3374】 String Problem (最小最大表示法+KMP)的更多相关文章

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

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

  2. kuangbin专题十六 KMP&&扩展KMP HDU3347 String Problem(最小最大表示法+kmp)

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

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

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

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

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

  5. hdu3374 String Problem【最小表示法】【exKMP】

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

  6. hdu3374 String Problem

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

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

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

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

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

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

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

随机推荐

  1. skynet是什么

         云风的skynet,定义为一个游戏服务器框架,用c + lua基于Actor模型实现.代码极其精简,c部分的代码只有三千行左右.      整个skynet框架要解决的核心问题是:把一个消息 ...

  2. 第二种方式读取并显示HDFS中的内容

    1.讀取HDFS内容的java客戶端代碼: package Hdfs; import java.io.InputStream; import java.net.URI; import org.apac ...

  3. addEventListener 用法

    addEventListener 用于注册事件处理程序,IE 中为 attachEvent,我们为什么讲 addEventListener 而不讲 attachEvent 呢?一来 attachEve ...

  4. c#简体繁体转换

     方法一已经亲测,使用正常,方法二貌似不能用. 方法一 /// <summary> /// 中文字符工具类 /// </summary> public static class ...

  5. c标准库中字符和数字转换的函数

    字符转换为数字: #include<stdlib.h> atoi();    将字符转换为整型                       例:char ch1;int i=atoi(ch ...

  6. IOS开发之KVC与KVO简述

    KVC:Key-Value Coding KVO:Key-Value Observing Person.m #import <Foundation/Foundation.h> @inter ...

  7. [转]Javascript 严格模式详解

    原文地址:http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html 一.概述 除了正常运行模式,ECMAscript 5添加 ...

  8. IO流中的文件创建并且写入读取

    package com.java.inoutputstreamDmeo.www; import java.io.File;import java.io.FileInputStream;import j ...

  9. 国庆第六日(2014年10月6日11:51:15),node-webkit,理财产品

    (1)node-webkit:一篇很好的入门文章.入门.系列. 在window下的打包和运行.大漠的一篇讲解文章 . (2)lighttable: 官网. (3)现在的理财产品,雨后春笋般冒出:宝点网 ...

  10. HeadFirst设计模式

    oo基础 抽象 封装 多态 继承 oo原则 封装变化 多用组合,少用继承 针对接口编程,不针对实现编程(策略模式) 为交互对象之间的松耦合设计而努力(观察者模式) 对扩展开放,对修改关闭(装饰者模式) ...