【题目链接】

点击打开链接

【算法】

EXKMP

【代码】

#include<bits/stdc++.h>
using namespace std;
#define MAXL 1000010 int i,N,l1,l2;
char s1[MAXL],s2[MAXL];
int extend[MAXL],next[MAXL]; template <typename T> inline void read(T &x) {
int f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) { if (c == '-') f = -f; }
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x%+'');
}
template <typename T> inline void writeln(T x) {
write(x);
puts("");
}
inline void getnext() {
int i=,pos,j;
next[] = l2;
while (s2[i] == s2[i+] && i + < l2) i++;
next[] = i;
pos = ;
for (i = ; i < l2; i++) {
if (i + next[i-pos] < pos + next[pos]) next[i] = next[i-pos];
else {
j = pos + next[pos] - i;
if (j < ) j = ;
while (i + j < l2 && s2[j] == s2[i+j]) j++;
next[i] = j;
pos = i;
}
}
} inline void exkmp() {
int i=,pos,j;
while (s1[i] == s2[i] && i < l1 && i < l2) i++;
extend[] = i;
pos = ;
for (i = ; i < l1; i++) {
if (i + next[i-pos] < pos + extend[pos]) extend[i] = next[i-pos];
else {
j = pos + extend[pos] - i;
if (j < ) j = ;
while (i + j < l1 && j < l2 && s1[i+j] == s2[j]) j++;
extend[i] = j;
pos = i;
}
}
} int main() { scanf("%s",&s1);
scanf("%s",&s2);
l1 = strlen(s1); l2 = strlen(s2); getnext();
exkmp(); for (i = ; i < l1; i++) {
if (!i) write(extend[i]);
else {
putchar(' ');
write(extend[i]);
}
} puts(""); return ; }

【CAIOJ 1178】 最长共同前缀长度的更多相关文章

  1. CIDR详解和ip最长地址前缀匹配

    1.CIDR是什么 无类域间路由(CIDR)编址方案 摒弃传统的基于类的地址分配方式,允许使用任意长度的地址前缀,有效提高地址空间的利用率. 就是一个ip加一个网络掩码,不过这个掩码不是之前只有3个值 ...

  2. lintcode :最长公共前缀

    题目 最长公共前缀 给k个字符串,求出他们的最长公共前缀(LCP) 样例 在 "ABCD" "ABEF" 和 "ACEF" 中,  LCP ...

  3. LeetCode Longest Common Prefix 最长公共前缀

    题意:给多个字符串,返回这些字符串的最长公共前缀. 思路:直接逐个统计同一个位置上的字符有多少种,如果只有1种,那么就是该位是相同的,进入下一位比较.否则终止比较,返回前缀.可能有一个字符串会比较短, ...

  4. leetcode-14最长公共前缀

    leetcode-14最长公共前缀 题目 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower& ...

  5. 最长公共前缀(java实现)

    题目: 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow& ...

  6. 【LeetCode】14. 最长公共前缀

    题目 编写一个函数来查找字符串数组中的最长公共前缀.如果不存在公共前缀,返回空字符串 "". 示例 1:输入: ["flower","flow&quo ...

  7. 最长公共前缀的golang实现

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 输入: ["flower","flow",&quo ...

  8. D - Milk Patterns (出现k次可重复的最长子串的长度)

    题目链接:https://cn.vjudge.net/contest/283743#problem/D 题目大意:给你n个数,然后问你出现m次的最长子串的长度. 具体思路:和上一篇博客的内容差不多,这 ...

  9. hdoj 2594 Simpsons’ Hidden Talents 【KMP】【求串的最长公共前缀后缀】

    Simpsons' Hidden Talents Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

随机推荐

  1. Codeforces 667C Reberland Linguistics【DFS】

    一道卡题意的题. 题目链接: http://codeforces.com/problemset/problem/667/C 题意: 一个串可以看成一个长度大于4的根,加上其后面的若干个相邻(in a ...

  2. HDU 4920 Matrix multiplication(bitset优化)

    题目链接 Matrix multiplication 求矩阵A和B相乘的结果. 因为答案只要对3取模,所以我们可以通过一些方法来加速计算. 我们对两个矩阵各开两个bitset,分别存储模3余1和模3余 ...

  3. Mac OS X 下安装python的MySQLdb模块

    参考资料: mac os x下python安装MySQLdb模块   http://www.codeif.com/post/1073/ MAC OSX使用Python安装模块有关问题  http:// ...

  4. 接阿里云oss有感

    看API,从头细看到尾,在这个过程中一定会找到你要找的东西.

  5. mysql 隔离级别与间隙锁等

    数据库隔离级 SQL标准中DB隔离级别有: read uncommitted:可以读到其它transaction 未提交数据 read committed:可以读到其它transaction 已提交数 ...

  6. iOS中创建自定义的圆角按钮

    iOS中很多时候都需要用到指定风格的圆角按钮,尽管UIButton提供了一个方式创建圆角按钮: + (id)buttonWithType:(UIButtonType)buttonType;//指定bu ...

  7. Android——ListView优化

    1.ListView基本概念 列表显示需要三个元素: ListView:用来展示列表的View. 适配器:用来把数据映射到ListView上 数据:具体的将被映射的字符串,图片或基本组件 适配器类型分 ...

  8. The user's guide what comes in the kernel Documentation directory

    The Linux IPMI Driver --------------------- Corey Minyard <minyard@mvista.com> <minyard@acm ...

  9. U盘 文件被隐藏解决办法

    原地址:http://www.deyi.com/thread-351635-1-1.html 方法:运行cmd( 在任意目录都行)单个文件 :attrib c:\"要修改的文件夹名字&quo ...

  10. python socket初探

    先看一段代码 import socket import sys import re def getServerContent(url): host_ip = socket.gethostbyname( ...