点击打开链接

Problem Description

Generally speaking, there are a lot of problems about strings processing. Now you encounter another such problem. If you get two strings, such as “asdf” and “sdfg”, the result of the addition between them is “asdfg”, for “sdf” is
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

For each case, there are two strings (the chars selected just form ‘a’ to ‘z’) for you, and each length of theirs won’t exceed 10^5 and won’t be empty.
 

Output

Print the ultimate string by the book.
 

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题真坑的更多相关文章

  1. 杭电acm 1076题

    水题,一个求闰年的题目,复习一下闰年的求法.... 1,如果能被4整除但不能被100整除的是闰年 2,能被400整除的是闰年 题目大意是:给定一个开始年份T以及一个正数N,要求求出从T开始,到了哪一年 ...

  2. 杭电acm 1037题

    本题应该是迄今为止最为简单的一道题,只有一组输入,输出也简单.... /****************************************** 杭电acm 1037题 已AC ***** ...

  3. 杭电acm 1038题

    本题比较简单,但是需要掌握几个小技巧,先上代码 /************************************* 杭电ACM 1038题,已AC ********************* ...

  4. 杭电acm 1049题

    一道水题..... 大意是一条1inch的虫子在一个n inch的盒子的底部,有足够的能够每一分钟往上爬u inch,但是需要休息一分钟,这期间会往下掉d inch,虫子爬到盒子口即认为结束.要求计算 ...

  5. 杭电acm 1033题

    Problem Description For products that are wrapped in small packings it is necessary that the sheet o ...

  6. 杭电acm 1015题

    马上要找工作了,锻炼下自己的写程序能力,不多说,上代码 /********************杭电acm 1015 已AC 在这个程序里,使用穷举法来实现,但是输出顺序需要安装字典的最大 来输出 ...

  7. 杭电ACM刷题(1):1002,A + B Problem II 标签: acmc语言 2017-05-07 15:35 139人阅读 评

    最近忙于考试复习,没有多少可供自己安排的时间,所以我利用复习之余的空闲时间去刷刷杭电acm的题目,也当对自己编程能力的锻炼吧. Problem Description I have a very si ...

  8. 杭电acm 1040题

    本题是一个非常简单的升序排序题目,但那时在做的时候把题目看错了,导致花费了大量的时间来检查为什么WA,最后发现题目看错了..... /********************************* ...

  9. 杭电acm 1098题

    Problem Description Ignatius is poor at math,he falls across a puzzle problem,so he has no choice bu ...

随机推荐

  1. jquery源码区块

  2. AtCoder Grand Contest 020

    A - Move and Win Time limit : 1sec / Memory limit : 512MB Score : 300 points Problem Statement A gam ...

  3. 解决Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.7

    一般情况下可能是文件格式有问题,将正确的文件内容替换掉错误的文件内容,不断地尝试,直到文件不报错,当然也有可能是下面的原因:下面是2.7.1版本的方法,其他类似) 或者是:进入该jar包指示的路径,删 ...

  4. NSUserDefaults 简介

    NSUserDefaults适合存储轻量级的本地数据,一些简单的数据(NSString类型的)例如密码,网址等,NSUserDefaults肯定是首选,但是如果我们自定义了一个对象,对象保存的是一些信 ...

  5. 【Luogu】P1472奶牛家谱(DP)

    题目链接 这是一道考思维的好题. 一开始设f[i][j]是i个点刚好j层的方案数,死活调不出来,看题解发现可以改为<=j层的方案数,最后输出f[n][m]-f[n][m-1]就好了. 对于计算考 ...

  6. 两种KMP题+KMP模版整理

    最近稍微看了下KMP,不是很懂他们大神的A题姿势,但是模版总该还是要去学的. 其中next数组的求法有两处区别. 第一种:求主串中模式串的个数.HDU2087 剪花布条和HDU4847 Wow! Su ...

  7. 刷题总结:排序机械臂(石室中学oj)(splay)

    题目: 题目描述 为了把工厂中高低不等的物品按从低到高排好序,工程师发明了一种排序机械臂.它遵循一个简单的排序规则,第一次操作找到最低的物品位置 P1,并把从左起第 1 个至第 P1 个之间的物品反序 ...

  8. poj 3109

                                                                                                         ...

  9. 转 C语言编译过程简介

    C语言编译过程简介 C语言编译过程简介 刚开始接触编程的时候,只知道照书敲敲代码,一直都不知道为什么在windows平台下代码经过鼠标那样点击几下,程序的结果就会在那个黑色的屏幕上.现在找了个机会将C ...

  10. 标准C程序设计七---57

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...