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 ...
随机推荐
- XML映射文件中关系映射
映射(多)对一.(一)对一的关联关系 1).使用列的别名 ①.若不关联数据表,则可以得到关联对象的id属性 ②.若还希望得到关联对象的其它属性.则必须关联其它的数据表 1.创建表: 员工表: DROP ...
- LeetCode(166) Fraction to Recurring Decimal
题目 Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- 图上两点之间的第k最短路径的长度 ACM-ICPC 2018 沈阳赛区网络预赛 D. Made In Heaven
131072K One day in the jail, F·F invites Jolyne Kujo (JOJO in brief) to play tennis with her. Howe ...
- list_for_each_entry()函数分析
list_for_each原型: #define list_for_each(pos, head) \ for (pos = (head)->next, prefetch(pos->nex ...
- UVa 10934 DP Dropping water balloons
首先想一下特殊情况,如果只有一个气球,我们要确定高度只能从下往上一层一层地测试,因为如果气球一旦爆了,便无法测出气球的硬度. 如果气球有无数个,那么就可以用二分的方法来确定. 一般地,用d(i, j) ...
- Linux内存cache/buffer剖析
查询linux系统中空闲内存/内存使用状态查看/剩余内存查看 如何计算内存的使用量及空闲量 物理已用内存 = 实际已用内存 - 缓冲 - 缓存 = 24752 - 283 ...
- PHP 修改配置文件后重启命名
centosPHP配置文件路径: /etc/php.ini 修改完配置文件后需要重启php服务: systemctl restart php-fpm
- 【bzoj4785】[Zjoi2017]树状数组 线段树套线段树
题目描述 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道基础的树状数组题.给出一个长度为 n 的数组 A,初始值都为 0,接下来进行 m 次操作 ...
- HDU——1799循环多少次(杨辉三角/动态规划/C(m,n)组合数)
循环多少次? Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- BZOJ2527 [Poi2011]Meteors 【整体二分 + 树状数组】
题目 Byteotian Interstellar Union有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1份相邻),第i份上有第Ai个国家的太空站. 这个星球经常会下 ...