题目大意

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. org.apache.catalina.webresources.Cache.backgroundProcess The background cache eviction process was unable to free [10] percent of the cache for Context [/filestore] - consider increasing the maximum s

    需要耐心啊,太急于求成,希望直接就得到解决方法了...以至于正确方法都已经出现了,我却没有耐心看下去,所以反而又耽误了不少时间.... 项目加载100+张图片,还有一个小的MP4,所以console警 ...

  2. 浅谈内核的Makefile、Kconfig和.config文件

    Linux内核源码文件繁多,搞不清Makefile.Kconfig..config间的关系,不了解内核编译体系,编译修改内核有问题无从下手,自己写的驱动不知道怎么编进内核,不知道怎么配置内核,这些问题 ...

  3. 【草稿】JS中如何操作时间

    如何声明时间变量 如何设置时间变量的时.分.秒.毫秒 如何根据字符串变量,声明指定的时间变量 如何比较两个时间变量 代码如下: $(function () { var d = new Date(); ...

  4. oracle 多表连接查询

    一.内连接(inner join (可简写为join)) 内连接查询操作列出与连接条件匹配的数据行,它使用比较运算符比较被连接列的列值. 1.等值连接:在连接条件中使用等于号(=)运算符比较被连接列的 ...

  5. day03_04 文件后缀及系统环境变量

    进入cmd,如果想直接切换盘符的话,操作如下 dir命令---查看目录下的文件及文件夹 cd命令---想进入某个目录,就是是双击文件夹进入目录的命令,按table键有神奇效果哦 cd ..命令---类 ...

  6. 如何用字体在网页中画icon

    一.用css雪碧图 1.简介 CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许将一个页面涉及到的所有零星图片都包含到一张大图中, 利用CSS的“background- ...

  7. [uiautomator篇] [4] 运行成功的日志打印---最后写一个脚本来实现

    Testing started at 18:23 ... 05/10 18:23:01: Launching ChangeTextBehaviorTestNo apk changes detected ...

  8. 矩阵快速幂在ACM中的应用

    矩阵快速幂在ACM中的应用 16计算机2黄睿博 首发于个人博客http://www.cnblogs.com/BobHuang/ 作为一个acmer,矩阵在这个算法竞赛中还是蛮多的,一个优秀的算法可以影 ...

  9. equal(),hashcode(),toString()方法的作用

    equal(),hashcode(),toString()方法的作用 这三个方法都是java.lang.Object的方法. equal();判断两对象是否相等hashcode();为对象在容器中添加 ...

  10. 【java基础 12】HashMap中是如何形成环形链表的?

    导读:经过前面的博客总结,可以知道的是,HashMap是有一个一维数组和一个链表组成,从而得知,在解决冲突问题时,hashmap选择的是链地址法.为什么HashMap会用一个数组这链表组成,当时给出的 ...