题目链接

题目意思: 给出两个字符串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 ;
}

最新文章

  1. (转)DOM appendHTML实现及insertAdjacentHTML
  2. Markdown 新手指南
  3. django authenticate
  4. Spring(3)—— Junit框架单元测试
  5. API文档的阅读
  6. Qt 信号和槽函数
  7. GitHub使用教程及常见错误解决
  8. cocos2d-x 图形绘制
  9. Delphi组件开发-在窗体标题栏添加按钮(使用MakeObjectInstance(NewWndProc),并处理好多消息)
  10. 使用AdvancedInstaller打包web工程设置tomcat端口的方法
  11. 一天搞定HTML----标签的嵌套规则06
  12. Chapter 7:Statistical-Model-Based Methods
  13. redis入门(05)redis的key命令
  14. U盘重装Win10系统视频教程
  15. java 不使用paint方法进行画图
  16. 微信小程序--代码构成---JSON 配置
  17. JSP页面&lt;%@ ...%&gt;是什么意思?
  18. 解决GitHub下载速度比较慢
  19. [UE4]蓝图调试
  20. Nginx基础配置指令

热门文章

  1. stringstream的使用 UVA 10815
  2. PHP jpgraph的一点小提示和方法
  3. Redis源码解析:17Resis主从复制之主节点的部分重同步流程及其他
  4. Django项目:CRM(客户关系管理系统)--45--37PerfectCRM实现King_admin添加用户时密码加密
  5. TZ_05_Spring_Transaction的纯注解开发
  6. iBaties对比hibernate
  7. JavaScript的原型链
  8. HTML 语法简要总结
  9. jeecms9自定义标签以及使用新创建的数据库表
  10. Tomcat中startup.bat启动无效