点击打开链接

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. mysql primary partition分区

    尝试把数据库一个表分区 ALTER TABLE user PARTITION BY RANGE(TO_DAYS(`date`)) ( PARTITION p1004 VALUES LESS THAN  ...

  2. python学习--学习时间属性的应用(time / datetime )

    #!/usr/bin/python # -*- coding:utf-8 -*- # import time # myd={1:'a',2:'b'}# for key,value in dict.it ...

  3. ccna学习指南第七版

    1.加电post自检    闪存查找ios 可随时从命令行进入设置模式,为此可在特权模式下输入setup    ctrl+c退出特权模式 6.2cli   命令行界面 进入cli router> ...

  4. AutoEncoder and DenoiseAutoEncoder

    AutoEncoder and DenoiseAutoEncoder 第一部分 首先我们将实现一个如上图结构的最简单的AutoEncoder. 加载数据 在这里,我们使用MNIST手写数据集来进行实验 ...

  5. [python学习篇][廖雪峰][2][高级函数] map 和reduce

    我们先看map.map()函数接收两个参数,一个是函数,一个是序列,map将传入的函数依次作用到序列的每个元素,并把结果作为新的list返回. 举例说明,比如我们有一个函数f(x)=x2,要把这个函数 ...

  6. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online

    A Live Love DreamGrid is playing the music game Live Love. He has just finished a song consisting of ...

  7. PHP分页类(较完美)

    <?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $lis ...

  8. 乘法逆元__C++

    在开始之前我们先介绍3个定理: 1.乘法逆元(在维基百科中也叫倒数,当然是 mod p后的,其实就是倒数不是吗?): 如果ax≡1 (mod p),且gcd(a,p)=1(a与p互质),则称a关于模p ...

  9. 【HDOJ5971】Wrestling Match(二分图,并查集)

    题意:有n个人,m场比赛,x个人为good player,y个人为bad player, 每场比赛两个人分分别为good和bad,问good和bad是否会冲突 1 ≤ N≤ 1000,1 ≤M ≤ 1 ...

  10. poj 2318 TOYS 点与矩形的关系

    题目链接 题意 有一个矩形盒子,\(n(n\leq 5e4)\)条线段将其分成了\(n+1\)个区域(每条线段的两个端点分别在矩形的上边和下边,且线段互不相交).现向盒子中扔\(m(m\leq 5e4 ...