题目大意

Description

给你一个字符集合,你从其中找出一些字符串出来. 希望你找出来的这些字符串的最长公共前缀*字符串的总个数最大化.

Input

第一行给出数字N.N在[2,1000000] 下面N行描述这些字符串,长度不超过20000 。保证输入文件不超过10MB

Output

输出一行一个整数代表字符串的最长公共前缀*字符串的总个数的最大值。

Sample Input

7
Jora de Sus
Orhei
Jora de Mijloc
Joreni
Jora de Jos
Japca
Orheiul Vechi

Sample Output

24

题解

KMP的妙用.

KMP除了直接进行字符串匹配以外, 还可以实现字符差匹配, 也就是不考虑单独的字符是否相同, 而是考虑相邻两个字符的差是否相同.

#include <cstdio>
#include <cctype> namespace Zeonfai
{
inline int getInt()
{
int a = 0, sgn = 1;
char c;
while(! isdigit(c = getchar()))
if(c == '-')
sgn *= -1;
while(isdigit(c))
a = a * 10 + c - '0', c = getchar();
return a *sgn;
}
} const int N = (int)2e5, M = (int)2e5; namespace KMP
{
int nxt[N]; inline void prework(int *str, int len)
{
nxt[1] = 0;
int p = 0;
for(int i = 2; i < len; ++ i)
{
for(; p && str[p + 1] - str[p] ^ str[i] - str[i - 1]; p = nxt[p]);
nxt[i] = str[p + 1] - str[p] == str[i] - str[i - 1] ? ++ p : p;
}
} inline int match(int *s, int sLen, int *t, int tLen)
{
int p = 0, res = 0;
for(int i = 1; i < tLen; ++ i)
{
for(; p && s[p + 1] - s[p] ^ t[i] - t[i - 1]; p = nxt[p]);
if(s[p + 1] - s[p] == t[i] - t[i - 1])
++ p;
if(p + 1 == sLen)
++ res, p = nxt[p];
}
return res;
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("XSY2336.in", "r", stdin);
#endif
using namespace Zeonfai;
int m = getInt(), n = getInt();
if(n == 1)
{
printf("%d\n", m);
return 0;
}
static int s[M], t[N];
for(int i = 0; i < m; ++ i)
t[i] = getInt();
for(int i = 0; i < n; ++ i)
s[i] = getInt();
KMP::prework(s, n);
printf("%d\n", KMP::match(s, n, t, m));
}

Codeforces 471 D MUH and Cube Walls的更多相关文章

  1. Codeforces Round #269 (Div. 2) D - MUH and Cube Walls kmp

    D - MUH and Cube Walls Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & % ...

  2. Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!

    D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...

  3. D - MUH and Cube Walls

    D. MUH and Cube Walls   Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant ...

  4. [codeforces471D]MUH and Cube Walls

    [codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...

  5. CodeForces 471D MUH and Cube Walls -KMP

    Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace from the zoo of ...

  6. codeforces MUH and Cube Walls

    题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得 a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k 就说b串在a串中出现过!最后输出 ...

  7. MUH and Cube Walls

    Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...

  8. CF471D MUH and Cube Walls

    Link 一句话题意: 给两堵墙.问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...

  9. CodeForces–471D--MUH and Cube Walls(KMP)

    Time limit         2000 ms  Memory limit  262144 kB Polar bears Menshykov and Uslada from the zoo of ...

随机推荐

  1. Leetcode 75. 颜色分类

    题目链接 https://leetcode-cn.com/problems/sort-colors/description/ 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们 ...

  2. 《鸟哥的Linux私房菜》学习笔记(3)——根文件系统

    一.Linux目录结构 rootfs:根文件系统,根是"/". 1./boot 系统启动相关的文件,如内核.intrd.以及grub(bootloader) root@hao:~# ...

  3. flask_关注者

    表的模型实现 class Follow(db.Model): __tablename__ = 'follows' follower_id = db.Column(db.Integer,db.Forei ...

  4. 加密javascript代码

    最近看了个js日历,里面用到了加密,看了下,自己也模仿做加密,现在只能加密一般的javascript语句 <!DOCTYPE html> <html> <meta htt ...

  5. 1px的实现

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. CornerStone使用教程(配置SVN,HTTP及svn简单使用)

    1.SVN配置 假设你公司svn地址为:svn://192.168.1.111/svn/ios,用户名:svnserver,密码:123456 1:填写主机地址 2:如果你的主机地址中有端口号,如为1 ...

  7. Linux 安装 tree命令

    通过yum在线安装tree包 yum install tree -y

  8. linux 复制部分文件到另外的文件夹

    show the command: |xargs -i cp {} ../ 或者指定目录 |xargs -i cp {} /home/peter

  9. 第4章--变量,作用域和内存问题 jquery

    4.1基本类型和引用类型的值 解析器要分析赋给变量的值是基本类型值还是引用类型的值 基本类型:undefined   null   boolean   number    string 引用类型的值: ...

  10. getattr、setattr、hasattr

    写一个演示类 class test(): title="验证getattr.setattr.hasattr方法" def run(self): return "run方法 ...