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. Cells Not Under Attack

    Cells Not Under Attack Vasya has the square chessboard of size n × n and m rooks. Initially the ches ...

  2. Anroid ListView分组和悬浮Header实现

    Anroid ListView分组和悬浮Header实现 分类: Android2014-01-27 12:26 6585人阅读 评论(13) 收藏 举报 listviewheadersection分 ...

  3. hrbustoj 2013 Play Game 2(博弈)

    注释在代码里 /* 1.若输入2 ~ 9 ,因为Stan 是先手,所以Stan 必胜 2.若输入10~18 ,因为Ollie 是后手,不管第一次Stan 乘的是什么,Stan肯定在 2 ~ 9 之间, ...

  4. dotnet webservice处理数据量过大,ajax请求返回500错误解决方案

    ajax请求webservice返回json数据,数据规模过大时ajax请求会得到500的响应,webservice+ajax处理大规模的数据需要在web.config中进行如下配置: <sys ...

  5. POJ 1185炮兵阵地 (状压DP)

    题目链接 POJ 1185 今天艾教留了一大堆线段树,表示做不动了,就补补前面的题.QAQ 这个题,我第一次写还是像前面HDU 2167那样写,发现这次影响第 i 行的还用i-2行那样,那以前的方法就 ...

  6. fn标签

    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>

  7. higncharts 去掉Highcharts.com链接

    将credits属性设为false credits: { enabled: false },

  8. A tutorial that will show you how to build an instant messaging app with Sinch.

    http://stackoverflow.com/questions/26247986/unsatisfiedlinkerror-couldnt-load-sinch-android-rtc-from ...

  9. highcharts分段显示不同颜色

    最近在做统计图的时候,碰到一个需求 类似如下: 就是在红色虚线框内的折线在不同区域用不同的颜色表示,并且是虚线. 开始定位为用highcharts库实现.确定用这个库后,开始在网上查资料,发现有类似的 ...

  10. mysql面试

    第一方面:30种mysql优化sql语句查询的方法 避免全表扫描: 1.where 及 order by 上建立索引.2.避免在 where 子句中使用!=或<>操作符3. select ...