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

String Problem

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

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
 
 
这个题我开始直接做,感觉很容易,但WA
 
WA代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

char str[1000005];
char str1[1000005];
int len;

int main(void)
{
int i;
int a1,a2;
while(scanf("%s",str)==1)
{
strcpy(str1,str);
len=strlen(str);
sort(str,str+len);
int k1=0,k2=0;
for(i=0;str[i]!='\0';i++)
{
if(str[i]==str[0])
k1++;
if(str[i]==str[len-1])
k2++;
}
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]==str[0])
{
a1=i;
break;
}
}
for(i=0;str1[i]!='\0';i++)
{
if(str1[i]==str[len-1])
{
a2=i;
break;
}
}
printf("%d %d %d %d\n",a1+1,k1,a2+1,k2);
}
return 0;
}

不知道哪里错了,如果谁知道,请留言!

后来到网上搜了一下发现别人是用next数组,根据l=len-next[len]为最小字符串找个数,根据最小表示法,与最大表示法,找初始位置。

//最小表示法

int minexp(char *s,int x)//x=strlen(s);
{
int i=0,j=1,k=0,t;
while(i<x&&j<x&&k<x)
{
t=s[(i+k)%x]-s[(j+k)%x];
if(t==0) k++;
else
{
if(t>0)
i+=k+1;
else
j+=k+1;
if(i==j)
j++;
k=0;
}
}
return i<j?i:j;
}

 
 
//最大表示法
 

int maxexp(char *s,int x)
{
int i=0,j=1,k=0,t;
while(i<x&&j<x&&k<x)
{
t=s[(i+k)%x]-s[(j+k)%x];
if(t==0) k++;
else
{
if(t<0) i+=k+1; //这里是最小表示法与最大表示法的区别。
else j+=k+1;
if(i==j) j++;
k=0;
}
}
return i<j?i:j;
}

AC代码

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

char pat[1000005];
int lenp,next[1000005];

void get_next()
{
int i,j;
next[0]=-1;
i=0;
j=-1;
while(i<lenp)
{
if(j==-1||pat[i]==pat[j])
{
i++;
j++;
next[i]=j;
}
else
j=next[j];
}
}
//最小表示
int minexp(char *s,int x)
{
int i=0,j=1,k=0,t;
while(i<x&&j<x&&k<x)
{
t=s[(i+k)%x]-s[(j+k)%x];
if(t==0) k++;
else
{
if(t>0)
i+=k+1;
else
j+=k+1;
if(i==j)
j++;
k=0;
}
}
return i<j?i:j;
}

//最大表示
int maxexp(char *s,int x)
{
int i=0,j=1,k=0,t;
while(i<x&&j<x&&k<x)
{
t=s[(i+k)%x]-s[(j+k)%x];
if(t==0) k++;
else
{
if(t<0) i+=k+1; //这里是区别
else j+=k+1;
if(i==j) j++;
k=0;
}
}
return i<j?i:j;
}

int main()
{
int ans,i,j,a,b;
while(scanf("%s",pat)!=EOF)
{
lenp=strlen(pat);
get_next();
a=minexp(pat,lenp);
b=maxexp(pat,lenp);
int l=lenp-next[lenp];
if(lenp%l==0)
{
ans=lenp/l;//cout<<lenp/l<<endl;;
}
else
{
ans=1;//printf("1\n");
}
printf("%d %d %d %d\n",a+1,ans,b+1,ans);
}
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+最大/最小表示)

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

  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. gridview如何隐藏一列数据,但又可以使用这列数据

    解决方案在RowCreated事件中书写如下代码 void GridView1_RowCreated(object sender, GridViewRowEventArgs e)    {       ...

  2. 极光推送集成——iOS10 接受消息问题及解决

    iOS10升级后极光推送发生了很大的变化,要求Xcode更新到8.0及以上版本才可以实现iOS10接受消息的方法 常见错误 这个问题困扰了我一天,辛亏好友盼神帮我解决,在此再次感谢盼神.一下是解决办法 ...

  3. 挂载了Cinder Volume的实例无法动态迁移排错

    现象:挂载了Cinder Volume的实例无法动态迁移 [root@node-5 nova]# tail -f compute.log 2016-01-13 16:36:12.870 18762 E ...

  4. thinkphp5.0入口文件

    入口文件 ThinkPHP采用单一入口模式进行项目部署和访问,无论完成什么功能,一个应用都有一个统一(但不一定是唯一)的入口. 应该说,所有应用都是从入口文件开始的,并且不同应用的入口文件是类似的. ...

  5. shell命令前后端切换和暂停

    Shell支持作用控制,有以下命令: 1. command& 让进程在后台运行 2. jobs 查看后台运行的进程编号 3 ctrl+z 然程序进入后台并且暂停 3. fg %n 让后台运行的 ...

  6. HDU 1540 POJ 2892 Tunnel Warfare

    线段树 区间合并 单点修改 区间查询.又是1秒钟构思,差错查了好久... ... 发现一个int型的定义成了char型,打脸. #include <stdio.h> #include &l ...

  7. input限定文件上传类型:Microsoft Office MIME types

    <input id = " " name = " " type = " file " accept=" ? ? ? &quo ...

  8. Ubuntu apt-get 详解

    一.常用的APT命令参数: apt-cache search package 搜索软件包 apt-cache show package  获取包的相关信息,如说明.大小.版本等 sudo apt-ge ...

  9. CSDN泄漏数据完整分析

    CSDN泄漏数据完整分析 2011-12-22 08:59:26 53391 次阅读 0 条评论 感谢mayee的投递 昨天CSDN的用户数据库被人在网上公布.我下载分析了下里面的数据,得出了一些很有 ...

  10. maven 阿里云节点,速度快

    修改maven根目录下的conf文件夹中的setting.xml文件. <mirrors> <mirror> <id>alimaven</id> < ...