【KMP】hdu1867(A + B for you again) 杭电java a题真坑
Problem Description
the tail substring of “asdf” and the head substring of the “sdfg” . However, the result comes as “asdfghjk”, when you have to add “asdf” and “ghjk” and guarantee the shortest string first, then the minimum lexicographic second, the same rules for other additions.
Input
Output
Sample Input
asdf sdfg
asdf ghjk
Sample Output
asdfg
asdfghjk
解题思路:题目就是字符串模式匹配,注意一些细节问题就能够了。
可是java写的就是无限超内存。今天心情不好。再交,就过了,这是无语了啊
import java.util.*;
class P1867{
static int[] next=new int[100005];
public static void main(String args[]){
int n,m,i,len1,len2;
String str1,str2;
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
str1=sc.next();
str2=sc.next();
len1=str1.length();
len2=str2.length();
n=kmp(str1,str2);
m=kmp(str2,str1);
if(n==m){
if(str1.compareTo(str2)>0){
System.out.println(str2+str1.substring(n,len1));
}else{
System.out.println(str1+str2.substring(n,len2));
}
}else if(n>m){
System.out.println(str1+str2.substring(n,len2));
}else{
System.out.println(str2+str1.substring(m,len1));
}
}
}
public static void set_next(String str){
int i=0,j=-1;
next[0]=-1;
int len=str.length();
while(i<len){
if(j==-1||str.charAt(i)==str.charAt(j)){
i++;
j++;
next[i]=j;///System.out.print(next[i]+" ");
}else{
j=next[j];
}
}
//System.out.println();
}
public static int kmp(String str1,String str2){
int i=0,j=0;
int len1=str1.length(),len2=str2.length();
set_next(str2);
while(i<len1){//System.out.print(j+" ");
if(j==-1||(j<len2&&str1.charAt(i)==str2.charAt(j))){
i++;
j++;//System.out.print("** ");
}else{
j=next[j];
}//System.out.print(j+"* ");
}//System.out.println();
if(i==len1){
return j;
}
return 0;
}
}
【KMP】hdu1867(A + B for you again) 杭电java a题真坑的更多相关文章
- 杭电acm 1076题
水题,一个求闰年的题目,复习一下闰年的求法.... 1,如果能被4整除但不能被100整除的是闰年 2,能被400整除的是闰年 题目大意是:给定一个开始年份T以及一个正数N,要求求出从T开始,到了哪一年 ...
- 杭电acm 1037题
本题应该是迄今为止最为简单的一道题,只有一组输入,输出也简单.... /****************************************** 杭电acm 1037题 已AC ***** ...
- 杭电acm 1038题
本题比较简单,但是需要掌握几个小技巧,先上代码 /************************************* 杭电ACM 1038题,已AC ********************* ...
- 杭电acm 1049题
一道水题..... 大意是一条1inch的虫子在一个n inch的盒子的底部,有足够的能够每一分钟往上爬u inch,但是需要休息一分钟,这期间会往下掉d inch,虫子爬到盒子口即认为结束.要求计算 ...
- 杭电acm 1033题
Problem Description For products that are wrapped in small packings it is necessary that the sheet o ...
- 杭电acm 1015题
马上要找工作了,锻炼下自己的写程序能力,不多说,上代码 /********************杭电acm 1015 已AC 在这个程序里,使用穷举法来实现,但是输出顺序需要安装字典的最大 来输出 ...
- 杭电ACM刷题(1):1002,A + B Problem II 标签: acmc语言 2017-05-07 15:35 139人阅读 评
最近忙于考试复习,没有多少可供自己安排的时间,所以我利用复习之余的空闲时间去刷刷杭电acm的题目,也当对自己编程能力的锻炼吧. Problem Description I have a very si ...
- 杭电acm 1040题
本题是一个非常简单的升序排序题目,但那时在做的时候把题目看错了,导致花费了大量的时间来检查为什么WA,最后发现题目看错了..... /********************************* ...
- 杭电acm 1098题
Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice bu ...
随机推荐
- MFC自绘按钮的实现,按钮动态效果
最近项目需要实现按钮的动态效果,多方学习,现在终于能实现一些功能了. 过程如下: 第一,新建一MFC对话框应用程序. 第二,删除自带按钮,并添加两个按钮,button1,button2,ID为IDB_ ...
- J2ee项目 编译依赖顺序
这儿有个帖子, 最后一个回复是: “我把我项目的libraries的"Order and Export"中的JRE与J2EE顺序换了一个问题解决”. 帖子地址: http://b ...
- Codeforces Round #459 (Div. 2)
A. Eleven time limit per test 1 second memory limit per test 256 megabytes input standard input outp ...
- Android中当前墙纸Wallpaper存放的位置
最近想做个应用保存当前墙纸,找了一下,发现当前墙纸的位置在. /System/users/0/wallpaper 没有后缀.导出来修改一下名字就可以看到图标了.比如改为png. 但是,这个目录要求系统 ...
- hibernate缓存详解
hibernate中提供了两级缓存,一级缓存是Session级别的缓存,它属于事务范围的缓存,该级缓存由hibernate管理,应用程序无需干预:二级缓存是SessionFactory级别的缓存,该级 ...
- 【Luogu】P3313旅行(树链剖分)
题目链接 动态开点的树链剖分qwq. 跟小奇的花园一模一样,不做过多讲解. #include<cstdio> #include<cstring> #include<cct ...
- 二进制<2>
位运算简介及实用技巧(二):进阶篇(1) ===== 真正强的东西来了! ===== 二进制中的1有奇数个还是偶数个 我们可以用下面的代码来计算一个32位整数的二进制中1的个数的奇偶性, ...
- BZOJ2326 [HNOI2011]数学作业 【矩阵快速幂】
题解 我们设f[i]表示前i个数模M意义下的答案 则f[i] = f[i - 1] * 100...0 + i[i是几位就有几个0] 可以写出矩阵递推式: 之后按位数分组矩乘就好了 #include& ...
- springboot中的controller注解没有生效
springboot中的controller注解没有生效 , 启动的Application类没有在controller的父目录或同级目录
- 模型表单ModleForm
官方文档网址 http://python.usyiyi.cn/documents/django_182/topics/forms/modelforms.html 模型表单的应用场景 如果你正在构建 ...