题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=1867

A + B for you again

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

kmp。。。

注意: 字符串A+B也可是B+A反正输出相加之后最短的那个,若相加之后长度相等输出字典序最小的那个。。

 #include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<map>
using std::cin;
using std::cout;
using std::endl;
using std::find;
using std::sort;
using std::map;
using std::pair;
using std::vector;
using std::multimap;
#define pb(e) push_back(e)
#define sz(c) (int)(c).size()
#define mp(a, b) make_pair(a, b)
#define all(c) (c).begin(), (c).end()
#define iter(c) decltype((c).begin())
#define cls(arr,val) memset(arr,val,sizeof(arr))
#define cpresent(c, e) (find(all(c), (e)) != (c).end())
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define tr(c, i) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
const int N = ;
typedef unsigned long long ull;
int next[N];
char str1[N], str2[N];
struct KMP {
int i, j, n, m;
inline void get_next(char *src) {
m = strlen(src);
for (i = , j = next[] = ; i < m; i++) {
while (j > && src[i] != src[j]) j = next[j - ];
if (src[i] == src[j]) j++;
next[i] = j;
}
}
inline int kmp_match(char *text, char *pat) {
n = strlen(text);
for (i = j = ; i < n; i++) {
while (j > && text[i] != pat[j]) j = next[j - ];
if (text[i] == pat[j]) j++;
}
return j;
}
}go;
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int p, q;
while (~scanf("%s %s", str1, str2)) {
go.get_next(str2);
p = go.kmp_match(str1, str2);
go.get_next(str1);
q = go.kmp_match(str2, str1);
if (p > q || (p == q && - == strcmp(str1, str2))) {
printf("%s%s\n", str1, str2 + p);
} else {
printf("%s%s\n", str2, str1 + q);
}
}
return ;
}

hdu 1867 A + B for you again的更多相关文章

  1. HDU 1867 A + B for you again ----KMP

    题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的 ...

  2. Hdu 1867 KMP

    题目链接 题目意思: 给出两个字符串a, b, 求最长的公共字串c, c是a的后缀,也是b的前缀. 本题没有具体说明哪个字符串是文本串和匹配串, 所以都要考虑 思路: 查找的时候, 当文本串结束的时候 ...

  3. HDU 1867 A + B for you again 字符匹配

    解题报告:给你两个字符串,让你连接起来,没有前后顺序,要求是长度最短优先,其次是字典序最小.这题我用的是KMP,做两次匹配,分别把第一次跟第二次输入的字符串放前面,然后比较两次得到的字符窜的长度和字典 ...

  4. HDU 1867 A + B for you again(KMP算法的应用)

    A + B for you again Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. HDU 2087 HDU 1867 KMP标准模板题

    贴两道题,其中HDU2087是中文题,故不解释题目, 思路是,一发KMP,但是特别处理最后一位的失配边为0,这样就可以保证“判断完成但是不多判断”. 第二题,很毒瘤的题,要求求出,给定字符串A,B能够 ...

  6. hdu 1867 A+B again for you

    A + B for you again Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  7. hdu 1867 kmp匹配

    #include<stdio.h> #include<string.h> #define N 100100 void getnext(int next[],char s[]) ...

  8. HDU 1867 A + B for you again KMP解决问题的方法

    这是一个典型问题KMP申请书. 结果求增加两个字符串.该法的总和是相同的前缀和后缀也是字符串的字符串,您将可以合并本节. 但是,这个问题是不是问题非常明确的含义,因为不是太清楚,外观这两个字符串的顺序 ...

  9. A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)

    Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...

随机推荐

  1. C# 枚举显示中文

    转自:http://www.cnblogs.com/yank/archive/2011/09/08/EnumDisplayInChinese.html   using System; using Sy ...

  2. Android添加权限大讲解

    对于新手来说,最烦恼的不是如何从网上下载到安卓项目,而是下载到的安卓项目不知道如何添加权限和要添加哪些权限. 现在就针对安卓的权限来讲解这些权限应该具体用在什么地方 首先在项目下找到 AndroidM ...

  3. Linux 之dhcp服务搭建

    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议 dhcp服务器端监控端口67 涉及的配置文件:/etc/dhcp/dhcpd ...

  4. Git 中级用户的25个提示

    我使用 Git 大约已经有18个月时间,自认为能很好地驾驭它了.但是当我们请到 GitHub 的 Scott Chacon 来到 LVS 公司(一个博彩/游戏软件供应商/开发商)做专场培训时,我在第一 ...

  5. Android IOS WebRTC 音视频开发总结(三九)-- win10升级为何要p2p

    本文主要介绍webrtc p2p的应用场景,文章来自博客园RTC.Blacker,支持原创,转载请说明出处. P2P最简单的解释就是两个客户端之间直接进行数据交互,不经过服务端转发. 最早接触P2P是 ...

  6. android中关闭软键盘

    /**隐藏软键盘**/ View view = getWindow().peekDecorView(); if (view != null) { InputMethodManager inputman ...

  7. nth-of-type和nth-child的区别

    看CSS3时发现了一个nth-of-type选择器,发现平时基本没见过用,就研究了一下,w3c是这样说明的: :nth-of-type(n) 选择器匹配属于父元素的特定类型的第 N 个子元素的每个元素 ...

  8. JQuery 的几个有用的技巧

    JQuery代码 /* 新窗口打开链接:JQuery filter attr * 禁止鼠标弹出右键菜单:DOM contextmenu * 回到页面顶端:DOM scrollTo * 动态更换Css样 ...

  9. Oracle存储过程知识汇总

    基本语法篇: CREATE OR REPLACE PROCEDURE 存储过程名 //CREATE OR REPLACE PROCEDURE 是一个SQL语句通知Oracle数据库去创建一个叫做ske ...

  10. Objective-C代码的文件扩展名