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. Puzzles

    Puzzles Barney lives in country USC (United States of Charzeh). USC has n cities numbered from 1 thr ...

  2. Android----基于多触控的图片缩放和拖动代码实现

    引自:http://www.codefans.net/articles/584.shtml 一个android中自定义的ImageView控制,可对图片进行多点触控缩放和拖动类,包括了对图片放大和图片 ...

  3. PAT (Advanced Level) 1050. String Subtraction (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  4. 上传数据插件 Easy Populate 遇到问题 ?langer=remove

    批量上传,出现如下错误提示: ADD NEW PRODUCT FAILED! – Model: no200 – SQL error. Check Easy Populate error log in ...

  5. HDU 5172 GTY's gay friends 线段树

    GTY's gay friends Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  6. iOS中4种判断网络请求的方式(系统状态栏、AFNetworking、Reachability、自定义)

    iOS 实时判断网络状态 方法一:利用系统状态栏判断网络状态 // 状态栏是由当前app控制的,首先获取当前app UIApplication *app = [UIApplication shared ...

  7. web项目编译出错时,原因之一,可能是build path 中order and Export引起

    build path中的order and Export,如果两个libarary中有相同功能的jar包,则编译器会选择顺序在前的jar包中相应的类作为编译所需. 所以,当项目jar包较多的时候,如果 ...

  8. UVA 11255 Necklace

    带颜色数限制的polya计数. 其实感觉一样了... #include<iostream> #include<cstdio> #include<cstring> # ...

  9. 《玩转Bootstrap(JS插件篇)》笔记

    导入JavaScript插件 不论是单独导入还一次性导入之前必须先导入jQuery库. 一次性导入 <script src="js/bootstrap.min.js"> ...

  10. tp框架设置 mysql数据库的端口号

    <?php return array( //'配置项'=>'配置值' SHOW_PAGE_TRACE=>true,//开启trace信息 'DB_TYPE' => 'mysql ...