HDU1867 - A + B for you again
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
#include <stdio.h>
#include <string.h>
int next[100005];
void getnext(char str[])
{
int i = 1,j = 0;
int len = strlen(str);
next [0] = -1;
while(i < len)
{
if(j == -1 || str[i] == str[j])
{
i++;
j++;
next[i] = j;
}
else
j = next[j];
}
}
int kmp(char str1[],char str2[])
{
int i= 0,j = 0;
int len1 = strlen(str1),len2 = strlen(str2);
getnext(str2);
while(i<len1 && j<len2)
{
if(j == -1 || str1[i] == str2[j])
{
i++;
j++;
}
else
j = next[j];
}
if(i == len1)
return j;
return 0;
}
int main()
{
int x,y;
char str1[100005],str2[100005];
while(scanf("%s%s",str1,str2)!=EOF)
{
x = kmp(str1,str2);
y = kmp(str2,str1);
if(x == y)
{
if(strcmp(str1,str2)>0)
{
printf("%s",str2);
printf("%s\n",str1+x);
}
else
{
printf("%s",str1);
printf("%s\n",str2+x);
}
}
else if(x>y)
{
printf("%s",str1);
printf("%s\n",str2+x);
}
else
{
printf("%s",str2);
printf("%s\n",str1+y);
}
}
return 0;
}
HDU1867 - A + B for you again的更多相关文章
- hdu1867之KMP
A + B for you again Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 【KMP】hdu1867(A + B for you again) 杭电java a题真坑
点击打开链接 Problem Description Generally speaking, there are a lot of problems about strings processing. ...
- KMP回顾学习
记住这张图,getnext就是对一个已知的待匹配的串进行分析,nex[i]表示当a[i]匹配失败后我能跳到哪里,继续尝试匹配,而不是每一次失败都从头再来,先来看看代码 const int maxn = ...
随机推荐
- 通过winrm使用powershell远程管理服务器
原文地址 在Linux中,我们可以使用安全的SSH方便的进行远程管理.但在Windows下,除了不安全的Telnet以外,从Windows Server 2008开始提供了另外一种命令行原创管理方式, ...
- 51nod——T1103 N的倍数
题目来源: Ural 1302 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 一个长度为N的数组A,从A中选出若干个数,使得这些数的和是N的倍数. ...
- sublime text 插件emmet快捷命令
原文链接:http://www.17yaobai.com/?p=255 语法: 后代:> 缩写:nav>ul>li <nav> <ul> <li> ...
- ajax传对象或者数组到后端
ajax是无法直接传送对象或者数组,有些人自己处理的话,能够把数据依照自己的标准连接成一个字符串,然后到后端处理.可是数据的不确定性.导致有可能会出错.并且麻烦 事实上有开源的包,能够直接解释成jso ...
- PHP之实现双向链表(代码篇)
<?php/** * PHP之实现双向链表 */class Hero{ public $pre=null; public $no; public $name; public $next=null ...
- java 命令行 编译 运行程序
学习java使用IDE前最好先用用命令行的javac.java来跑一跑简单的程序,这样能够熟悉一下包管理对.class文件路径的影响. 我们先写一段简单的代码: package com.csdn.lk ...
- ETL (数据仓库技术)
ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载(load)至目的端的过程.ETL一词较常用在数 ...
- 如何让 ssh 允许以 root 身份登录
默认情况下,Pack 上的 root 用户不能用通过密码来远程登录,可以用一下命令来做:(注意要在 root 权限下) sed -i 's/PermitRootLogin\swithout-passw ...
- 车牌识别(end-to-end-for-chinese-plate-recognition)项目搭建基于Mxnet(Python 3.5)
最近看到geihub上有个车牌识别的项目,感觉很有意思,从上面fork了一下弄到本地自己跑了下.在安装过程中遇到了一些问题,记录如下. 项目github连接:https://github.com/sz ...
- Android4.0.4-在build.prop中添加属性的方法【转】
本文转载自:http://blog.csdn.net/imyfriend/article/details/8939964 1.在*.rc文件中用setprop添加,例如在源码android4.0\sy ...