【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 ...
随机推荐
- doc下设置永久环境变量的好方法
http://www-2w.blog.163.com/blog/static/97931518201021211123267/ 需要查看命令具体实现:setx machine “%path%”. 配置 ...
- 2018 “百度之星”程序设计大赛 - 初赛(B)
degree Accepts: 1581 Submissions: 3494 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 1310 ...
- netcore命令行部署|跨域问题
1.在hosting中修改发布端口号,如遇见不识别IP则改成*再用命令行运行 { "server.url": "http://*:8089"} 3.给接口开外网 ...
- 【Luogu】P2447外星千足虫(高斯消元)
题目链接 高斯消元解%2意义下的方程,Bitset优化一下. 在消的过程中就能顺便把有解的第一问求出来,记录一下访问过的最大行. #include<cstdio> #include< ...
- [LOJ#526]「LibreOJ β Round #4」子集
[LOJ#526]「LibreOJ β Round #4」子集 试题描述 qmqmqm有一个长为 n 的数列 a1,a2,……,an,你需要选择集合{1,2,……,n}的一个子集,使得这个子集中任意两 ...
- linux tomcat安装(二)
安装步骤: 1.下载 Tomcat 下载 apache-tomcat-7.0.69.tar.gz(官方网址:http://tomcat.apache.org/) 2.解压 Tomcat 解压 apac ...
- 【前端学习笔记】2015-09-10~~~~ css层叠样式表~~格式
关于颜色的定义: 有几种方式:1.直接接英文单词 2.#16进制 3.rgb(100%:0%:0%)或者里面写像素,此处百分号不能省略. 加多个属性时,用“;”分开 前面如果是定义标签的样式时, ...
- python并发之concurrent.futures
concurrent:并发 Python标准库为我们提供了threading和multiprocessing模块编写相应的多线程/多进程代码.从Python3.2开始,标准库为我们提供了concurr ...
- 【BZOJ1579】Revamping Trails(分层图,最短路,堆)
题意:每天,农夫John需要经过一些道路去检查牛棚N里面的牛. 农场上有M(1<=M<=50,000)条双向泥土道路,编号为1..M. 道路i连接牛棚P1_i和P2_i (1 <= ...
- 自己写的微信小程序炸金花简单版
app.json: { "pages":[ "pages/index/index" ], "window":{ "navigati ...