hdu 1867 A + B for you again
2015-07-06 20:24
417 查看
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1867
kmp。。。
注意: 字符串A+B也可是B+A反正输出相加之后最短的那个,若相加之后长度相等输出字典序最小的那个。。
kmp。。。
注意: 字符串A+B也可是B+A反正输出相加之后最短的那个,若相加之后长度相等输出字典序最小的那个。。
[code]#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 = 100010; typedef unsigned long long ull; int next ; char str1 , str2 ; struct KMP { int i, j, n, m; inline void get_next(char *src) { m = strlen(src); for (i = 1, j = next[0] = 0; i < m; i++) { while (j > 0 && src[i] != src[j]) j = next[j - 1]; if (src[i] == src[j]) j++; next[i] = j; } } inline int kmp_match(char *text, char *pat) { n = strlen(text); for (i = j = 0; i < n; i++) { while (j > 0 && text[i] != pat[j]) j = next[j - 1]; 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 && -1 == strcmp(str1, str2))) { printf("%s%s\n", str1, str2 + p); } else { printf("%s%s\n", str2, str1 + q); } } return 0; }
相关文章推荐
- POJ 3250 Bad Hair Day 模拟单调栈
- Rails 异常处理 && 性能
- 生产者与消费者模型 wait() · notify() 方法的使用
- iredmail安装问题
- leetcode Contains Duplicate II 哈希表
- static变量在Main函数之前执行
- tail命令
- 如何在.net4.0中使用.net4.5的async/await
- AIX 5L 系统管理技术 —— 存储管理——物理卷
- Adobe Air移动开发本人体会
- Factorial Trailing Zeroes
- hive执行计划EXPLAIN
- 两个有序数组,从中各取一个组成pair,求和最小的前K个pair(杨氏矩阵top k问题)
- handler和runOnMainThread更新界面问题
- 如何设置SSH服务终端的超时时间或不超时
- pthread_cond_signal和pthread_cond_wait简介
- Exchange2010配置-多邮箱搜索发现 推荐
- 翻墙用青蛙加速器,上google,youtube,gmail必备
- climbing-stairs
- 正确使用Block避免Cycle Retain和Crash