题目连接

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 ;
}

最新文章

  1. 关于jsp的内置对象request和response的重定向和转化(待补充)
  2. zlib压缩一个文件为gzip格式
  3. B2B多商铺初期权限数据库设计
  4. PLSQL 的简单命令之三
  5. How to create UrlSlug in Asp.Net MVC
  6. SSH基础(2)
  7. mysqli 扩展库的预处理技术(mysqli_stmt)
  8. JDBC学习入门
  9. Lowest Common Ancestor of a Binary Search Tree (BST)
  10. unity3D学习序幕
  11. Linux_破解密码-营救模式
  12. 传统平面广告已OUT出局,VR全景异军突起——VR全景智慧城市
  13. phpstorm本地怎么上传到服务器
  14. js中uuid不被识别
  15. Tomcat简单优化
  16. VMware虚拟机扩展Ubuntu系统磁盘空间
  17. cf352E Jeff and Brackets dp+矩阵快速幂(加法+min运算)
  18. php中的一些不常见的问题foreach/in_array[开发篇]
  19. new Date()浏览器兼容性问题
  20. onsubmit 事件

热门文章

  1. Change screensaver through registry
  2. 8051学习笔记——IIC与EEPROM实验
  3. python --那些你应该知道的知识点
  4. js 函数function的几种形式
  5. 【qt4.8.6】qt-everywhere-opensource-src-4.8.6静态库编译,搭建vs2010 + Qt4.8.6环境
  6. C++ Builder技巧集锦
  7. java中使用mysql
  8. poj3020
  9. WAMP环境下访问PHP提示下载PHP文件
  10. WWDC————苹果全球开发者大会