Hdu 1867 KMP
2024-08-30 11:38:19
题目意思: 给出两个字符串a, b, 求最长的公共字串c, c是a的后缀,也是b的前缀. 本题没有具体说明哪个字符串是文本串和匹配串, 所以都要考虑
思路: 查找的时候, 当文本串结束的时候, 返回匹配串的位
/*************************************************************************
> File Name: 1867.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年05月15日 星期四 11时24分11秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define MAX_N (100000+5) char a[MAX_N], b[MAX_N];
int f[MAX_N<<];
void getFail(char *); int
find (char *T, char *P) {
int n = strlen(T);//m = strlen(P);
getFail(P);
int j = ;
for (int i = ; i < n; i++) {
while (j && T[i] != P[j]) j = f[j];
if (P[j] == T[i]) j++;
} return j;
} void
getFail(char *P) {
int m = strlen(P);
f[] = f[] = ;
for (int i = ; i < m; i++) {
int j = f[i];
while (j && P[i] != P[j]) j = f[j];
f[i+] = P[i] == P[j] ? j+ : ;
}
} int
main(void) {
while (~scanf("%s %s", a, b)) {
// int len1 = strlen(a);
// int len2 = strlen(b);
int cnt = find(a, b);
// cout << cnt << endl;
int cnt2 = find(b, a);
// cout << cnt2 << endl;
if (cnt > cnt2) {
printf("%s", a);
printf("%s\n", b+cnt);
} else if (cnt < cnt2){
printf("%s", b);
printf("%s\n", a+cnt2);
} else {
if (strcmp(a, b) < ) {
printf("%s", a);
printf("%s\n", b+cnt);
} else {
printf("%s", b);
printf("%s\n", a+cnt2);
}
}
} return ;
}
最新文章
- (转)DOM appendHTML实现及insertAdjacentHTML
- Markdown 新手指南
- django authenticate
- Spring(3)—— Junit框架单元测试
- API文档的阅读
- Qt 信号和槽函数
- GitHub使用教程及常见错误解决
- cocos2d-x 图形绘制
- Delphi组件开发-在窗体标题栏添加按钮(使用MakeObjectInstance(NewWndProc),并处理好多消息)
- 使用AdvancedInstaller打包web工程设置tomcat端口的方法
- 一天搞定HTML----标签的嵌套规则06
- Chapter 7:Statistical-Model-Based Methods
- redis入门(05)redis的key命令
- U盘重装Win10系统视频教程
- java 不使用paint方法进行画图
- 微信小程序--代码构成---JSON 配置
- JSP页面<;%@ ...%>;是什么意思?
- 解决GitHub下载速度比较慢
- [UE4]蓝图调试
- Nginx基础配置指令
热门文章
- stringstream的使用 UVA 10815
- PHP jpgraph的一点小提示和方法
- Redis源码解析:17Resis主从复制之主节点的部分重同步流程及其他
- Django项目:CRM(客户关系管理系统)--45--37PerfectCRM实现King_admin添加用户时密码加密
- TZ_05_Spring_Transaction的纯注解开发
- iBaties对比hibernate
- JavaScript的原型链
- HTML 语法简要总结
- jeecms9自定义标签以及使用新创建的数据库表
- Tomcat中startup.bat启动无效