hdu 1867 A + B for you again
题目连接
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的更多相关文章
- HDU 1867 A + B for you again ----KMP
题意: 给你两个字符串,输出他们合并之后的字符串,合并的时候把A的后缀和B的前缀重叠合(或者把A的前缀和B的后缀重合).要求合并后的串既包含A右包含B, 且使得合并后的字符串尽量短,其次是使得合并后的 ...
- Hdu 1867 KMP
题目链接 题目意思: 给出两个字符串a, b, 求最长的公共字串c, c是a的后缀,也是b的前缀. 本题没有具体说明哪个字符串是文本串和匹配串, 所以都要考虑 思路: 查找的时候, 当文本串结束的时候 ...
- HDU 1867 A + B for you again 字符匹配
解题报告:给你两个字符串,让你连接起来,没有前后顺序,要求是长度最短优先,其次是字典序最小.这题我用的是KMP,做两次匹配,分别把第一次跟第二次输入的字符串放前面,然后比较两次得到的字符窜的长度和字典 ...
- 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 ...
- HDU 2087 HDU 1867 KMP标准模板题
贴两道题,其中HDU2087是中文题,故不解释题目, 思路是,一发KMP,但是特别处理最后一位的失配边为0,这样就可以保证“判断完成但是不多判断”. 第二题,很毒瘤的题,要求求出,给定字符串A,B能够 ...
- 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 ...
- hdu 1867 kmp匹配
#include<stdio.h> #include<string.h> #define N 100100 void getnext(int next[],char s[]) ...
- HDU 1867 A + B for you again KMP解决问题的方法
这是一个典型问题KMP申请书. 结果求增加两个字符串.该法的总和是相同的前缀和后缀也是字符串的字符串,您将可以合并本节. 但是,这个问题是不是问题非常明确的含义,因为不是太清楚,外观这两个字符串的顺序 ...
- A + B for you again HDU - 1867(最大前缀&最大后缀的公共子缀&kmp删除法)
Problem Description Generally speaking, there are a lot of problems about strings processing. Now yo ...
随机推荐
- 使用PetaPoco ORM 框架分页查询
通过在派生的Repository中调用GetPagingEntities方法来获取分页数据,并返回由PagingDataSet<T>封装分页集合,例如: Public PagingData ...
- 洛谷P2728 纺车的轮子 Spinning Wheels
P2728 纺车的轮子 Spinning Wheels 29通过 66提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交 讨论 题解 最新讨论 暂时没有讨论 题目背景 一架纺车 ...
- No.001 Two Sum
Two Sum Total Accepted: 262258 Total Submissions: 1048169 Difficulty: Easy Given an array of integer ...
- ubuntu解压zip文件乱码问题
我的zip文件里的内容是有中文名,也有密码,在网上找到几种解决办法只有一种可以,所以在这里记录一下: 首先是安装7zip来解压,7zip的解决办法在这里,但是无法解决我的问题,仍然有乱码问题 最后是在 ...
- hdu 3336【Count the string】(KMP)
一道字符串匹配的题目,仅仅借此题练习一下KMP 因为这道题目就是要求用从头开始的n个字符串去匹配原来的字符串,很明显与KMP中求next的过程很相似,所以只要把能够从头开始匹配一定个数的字符串的个数加 ...
- SQL常用日期转换
0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy ...
- 使用python脚本实现基于指定字符串的文本排序
朋友用ansible导出了一个文件,文件中包含上千台机器的磁盘信息,他想要知道哪些机器最需要赶紧扩磁盘.思路是,按剩余磁盘空间百分数,从小到大对文本内容重新排序.下面是具体实现. 源文件ip.txt的 ...
- APUE第一章_课后习题
/* 未完成的:1.5 不过在下文中已经给出了解答. */ 1.1 在系统上查证,除根目录外,目录.和..是不同的 ans:这个很容易,用vim打开.和..就可以看到区别. 1.2 分析程序清单1-4 ...
- grub2
手工启动 set root(hd0,msdos7) linux /boot/vmlinuz-3.9.8-300.fc19.i686.PAE root=/dev/sda7 initrd /boo ...
- Chrome不能登录和同步的解决方法
打开 C:\Windows\System32\drivers\etc 下的 hosts文件 #SmartHosts START #Google Services START .docs.google. ...