String Problem

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

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

题目链接:HDU 3374

求最小与最大字典序的所需左移次数和出现次数,左移次数用两倍的s来匹配,在计数的时候把第一次匹配成功的开头位置记录下来即可,计数过程中i要小于len*2-1,比如abcde拿来当主串扩展两倍后变成abcdeabcde,其实中间一共左移所生成的串只有len-1个即到edbde就结束了,因此不能再去匹配最后一个e,不然像abcde本身拿去匹配这个串就出现了两次了……,代码写起来比较麻烦,顺便复习一下好久没敲了的KMP搜索函数。

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=1000010;
char s[N],fir[N],las[N],temp[N<<1];
int nxta[N],nxtb[N];
int len,L;
void init()
{
CLR(s,0);
CLR(fir,0);
CLR(las,0);
CLR(nxta,0);
CLR(nxtb,0);
CLR(temp,0);
}
void getnext(char s[],int nxt[])
{
int j=0,k=nxt[0]=-1;
len=strlen(s);
while (j<len)
{
if(k==-1||s[j]==s[k])
nxt[++j]=++k;
else
k=nxt[k];
}
}
int minp()
{
int i=0,j=1,k=0;
while (i<len&&j<len&&k<len)
{
int t=s[(i+k)%len]-s[(j+k)%len];
if(!t)
++k;
else
{
if(t>0)
i+=(k+1);
else
j+=(k+1);
if(i==j)
++j;
k=0;
}
}
return i<j?i:j;
}
int maxp()
{
int i=0,j=1,k=0;
while (i<len&&j<len&&k<len)
{
int t=s[(i+k)%len]-s[(j+k)%len];
if(!t)
++k;
else
{
if(t>0)
j+=(k+1);//要把这里i、j互换即可,其他不变
else
i+=(k+1);
if(i==j)
++j;
k=0;
}
}
return i<j?i:j;
}
pii kmp_count(char s[],char p[],int nxt[])
{
int i=0,j=0;
pii r;
r.first=-1,r.second=0;
while (i<L-1&&j<len)
{
if(s[i]==p[j]||j==-1)
{
++i;
++j;
}
else
j=nxt[j];
if(j==len)
{
if(r.first==-1)//只记录第一次的出现位置
r.first=i-j+1;
++r.second;
j=nxt[j];
}
}
return r;
}
int main(void)
{
int i,j,k;
while (~scanf("%s",s))
{
len=strlen(s);
L=len<<1;
int minm=minp();
int maxm=maxp();
strcpy(temp,s);
strcat(temp,s);
int cnt;
for (cnt=0,i=minm; cnt<len; ++i,++cnt)
fir[cnt]=temp[i];
for (cnt=0,i=maxm; cnt<len; ++i,++cnt)
las[cnt]=temp[i];
getnext(fir,nxta);
getnext(las,nxtb);
pii a=kmp_count(temp,fir,nxta);
pii b=kmp_count(temp,las,nxtb);
printf("%d %d %d %d\n",a.first,a.second,b.first,b.second);
init();
}
return 0;
}

HDU 3374 String Problem(KMP+最大/最小表示)的更多相关文章

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

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

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

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

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

    Problem Description Give you a string with length N, you can generate N strings by left shifts. For ...

  4. HDU 3374 String Problem(最大最小表示+KMP)题解

    题意:给你一个字符串,这个字符串可以这样操作:把第一个字符放到最后一个形成一个新的字符串,记原式Rank为1,每操作一步Rank+1,问你这样操作得出的最小字典序的字符串的Rank和这样的字符串有几个 ...

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

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

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

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

  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+最大最小表示法)

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

随机推荐

  1. 【HTTP协议】响应头中的Content-Length和Transfer-Encoding

    来源: http://blog.csdn.net/superhosts/article/details/8737434 http://bbs.csdn.net/topics/390384017 对于h ...

  2. document.createElement

    document.createElement()的用法 document.createElement()是在对象中创建一个对象,要与appendChild() 或 insertBefore()方法联合 ...

  3. 铺地毯(luogu 1003)

    题目描述 为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯.一共有 n 张地毯,编号从 1 到n .现在将这些地毯按照编号从小到大的顺序平行于 ...

  4. Android实现网络音乐播放器

    本文是一个简单的音乐播放器 布局代码 <?xml version="1.0" encoding="utf-8"?> <RelativeLayo ...

  5. svn 日志版本回滚

    [root@v01 online]# svn diff -r 9:8 Index: index.html =============================================== ...

  6. 在MVC3或asp.net中修改KindEditor实现上传图片时添加水印

    主要修改两个文件:image.js和upload_json.ashx文件. 一.修改image.js文件 打开kindeditor/plugins/image目录下的image.js文件,找到 '&l ...

  7. 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)

    Problem C: The Trip Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 19  Solved: 3[Submit][Status][Web ...

  8. 面向服务的体系结构(SOA)——(4)对于服务的理解

    在接触SOA之前对"服务"这个词没有太多的感触,无非就是为别人做事情么.当然在SOA中这个词也脱离不了这层基本的涵义,但是具体到实际的架构中对这个词的理解就更加深刻了.本文就以&q ...

  9. Java Hour 51 CheckStyle

    Plan List: 1 mysql 相关 2 java code style 安装体验 1 直接从eclipse install new software 安装,莫名奇妙失败,提示为缺少依赖项. 这 ...

  10. 布局文件中fill_parent、match_parent和wrap_content有什么区别?

    fill_parent 宽度或者高度 布满整个屏幕从Android 2.2开始fill_parent改名为match_parent.wrap_content布局元素将根据内容更改大小.