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. UESTC 1215 (思维题 旋转)

    Secrete Master Plan Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Othe ...

  2. [Android Pro] android Flag介绍

    一些Flag的介绍 窗口之后的内容变暗. public static final int FLAG_DIM_BEHIND       = 0x00000002; 窗口之后的内容变模糊. public ...

  3. Mybatis if判断的坑

    具体情况参考这两篇文章: http://cheng-xinwei.iteye.com/blog/2008200 http://www.cnblogs.com/tv151579/p/3297691.ht ...

  4. Javascript操作剪切板数据(支持IE、Chrome、360、搜狗),亲测!

    clipboarddata只能在IE浏览器中使用,在chrome下会提示对象未定义!以下的方法支持IE.Chrome.360.搜狗等浏览器,其它浏览器还未验证. <!DOCTYPE html&g ...

  5. WordPress主题制作函数

    WordPress基本模板文件 一套完整的WordPress模板应至少具有如下文件: style.css: CSS(样式表)文件 index.php: 主页模板 archive.php: Archiv ...

  6. PHP CI框架学习笔记-分页实现程序

    视图html  <div id="body"> <form action="/index.php/search/index/" method= ...

  7. CodeForces 222B Cosmic Tables

    Cosmic Tables Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  8. 利用crontab定时备份数据库为sql文件

    测试: 每1分钟执行1次,利用crontab */1 * * * * /opt/nginx/db_bak.sh 每天的0点0分把数据库备份为日期格式 0 0 * * * /opt/nginx/db_b ...

  9. 简单几何(直线与圆的交点) ZOJ Collision 3728

    题目传送门 题意:有两个一大一小的同心圆,圆心在原点,大圆外有一小圆,其圆心有一个速度(vx, vy),如果碰到了小圆会反弹,问该圆在大圆内运动的时间 分析:将圆外的小圆看成一个点,判断该直线与同心圆 ...

  10. ! cocos2d 预编译重复

    由于预编译文件重复,导致下面的类没有被编译,所以,在写代码的时候也没有提示还报错,说LoadingScene没有定义. #ifndef __HELLOWORLD_SCENE_H__ #define _ ...