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. GuideActivity.java引导界面:

    这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api. 而viewpager就是其中之一利用它,我们可以做很多事情,从最简单的导航,到页面菜单等等.那 ...

  2. WordPress常用插件

    1.Remove Open Sans font Link from WP core 由于Wordpress后台外链加载了谷歌字体(代码位置在wordpress\wp-includes\script-l ...

  3. UIApearance

    转载自:http://www.cocoachina.com/ios/20150723/12671.html 文章开头先援引一下Mattt Thompson大神在UIApearance里的一句话吧: 1 ...

  4. eclipse中创建NDK和JNI开发环境最简单配置方法

    一.使用环境 1.windows64位操作系统 2.ADT为adt-bundle-windows-x86_64-20130917 3.NDK为android-ndk-r9b 二.配置生成头文件.h ⒈ ...

  5. localStorage、sessionStorages 使用

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage.sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有 ...

  6. LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods

    我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Intersect操作和Top/Bottom操作和Paging操作和SqlMethods操作 . Union Al ...

  7. String类的两种赋值

    java.lang包是java的默认引入包,所以我们不需显式地导包. String s1 = new String("字符串");//创建2个字符串对象,堆中一个,字符串常量池中一 ...

  8. arcengine C#关于动态添加图层

    动态加载影像图层为例 研究了两三天算是弄出来了.本例适合影像数据量特别的大程序使用,可以动态的添加删除影像数据,还有不完善的地方,不过基本满足要求. 1.首先得到关键点的图层 m_Map = axMa ...

  9. redis sentinel 集群监控 配置

    环境: ip  172.16.1.31 26379  redis sentinel ip  172.16.1.30 6379   主 1 ip  172.16.1.31 6380   从 1 ip   ...

  10. DNS架设准备+申请领域查询授权

    1. 架设DNS服务器首先我们得安装一下的软件[root@bogon ~]# rpm -qa | grep ^bindbind-libs-9.8.2-0.37.rc1.el6.i686 <==给 ...