Codeforces 471 D MUH and Cube Walls
题目大意
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的更多相关文章
- 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 & % ...
- Codeforces Round #269 (Div. 2)-D. MUH and Cube Walls,KMP裸模板拿走!
D. MUH and Cube Walls 说实话,这题看懂题意后秒出思路,和顺波说了一下是KMP,后来过了一会确定了思路他开始写我中途接了个电话,回来kaungbin模板一板子上去直接A了. 题意: ...
- D - MUH and Cube Walls
D. MUH and Cube Walls Polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant ...
- [codeforces471D]MUH and Cube Walls
[codeforces471D]MUH and Cube Walls 试题描述 Polar bears Menshykov and Uslada from the zoo of St. Petersb ...
- 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 ...
- 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串中出现过!最后输出 ...
- MUH and Cube Walls
Codeforces Round #269 (Div. 2) D:http://codeforces.com/problemset/problem/471/D 题意:给定两个序列a ,b, 如果在a中 ...
- CF471D MUH and Cube Walls
Link 一句话题意: 给两堵墙.问 \(a\) 墙中与 \(b\) 墙顶部形状相同的区间有多少个. 这生草翻译不想多说了. 我们先来转化一下问题.对于一堵墙他的向下延伸的高度,我们是不用管的. 我们 ...
- CodeForces–471D--MUH and Cube Walls(KMP)
Time limit 2000 ms Memory limit 262144 kB Polar bears Menshykov and Uslada from the zoo of ...
随机推荐
- 在windows7 32ibt安装MongoDB数据库的方法及连接失败解决方案
参考 https://www.cnblogs.com/cnblogs-jcy/p/6734889.html http://yunkus.com/mongodb-install-config-in-wi ...
- freertos知识点笔记——队列、二值信号量、计数信号量
队列1.队列queue通常用于任务之间的通信,一个任务写缓存,另一个任务读缓存.队列还会有等待时间,2.阻塞超时时间.如果在发送时队列已满,这个时间即是任务处于阻塞态等待队列空间有效的最长等待时间.如 ...
- 【HIHOCODER 1181】欧拉路·二
描述 在上一回中小Hi和小Ho控制着主角收集了分散在各个木桥上的道具,这些道具其实是一块一块骨牌. 主角继续往前走,面前出现了一座石桥,石桥的尽头有一道火焰墙,似乎无法通过. 小Hi注意到在桥头有一张 ...
- 并查集:CDOJ1593-老司机破阵 (假的并查集拆除)
老司机破阵 Time Limit: 4500/1500MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Problem Descri ...
- js中xml文件加载
- Nginx快速安装
登录nginx官网,点击download 点击稳定版本 复制箭头上面的内容 vim /etc/yum.repos.d/nginx.repo 修改yum文件 将复制的内容粘贴上去 yum list | ...
- [python] 求大神解释下 面向对象中方法和属性
面向对象中 类方法 实例方法 类属性 实例属性该如何理解呢?
- luogu2393 yyy loves Maths II
使用long double #include <iostream> #include <cstdio> using namespace std; long double ans ...
- [转] 查看 SELinux状态及关闭SELinux
本文转载自: http://bguncle.blog.51cto.com/3184079/957315 查看SELinux状态: 1./usr/sbin/sestatus -v ##如果SE ...
- 薛XX后代的IQ CSU1597【循环节】或【快速幂】
薛先生想改变后代的IQ,为此他发明了一种药,这种药有三种属性:A, B,P.他父亲的智商为X,薛先生的智商为Y,用了这种药之后,薛先生的孩子的智商就可以变为(AX+BY) mod P.后代的智商以此类 ...