题目大意

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. 模拟ajax请求爬取微博

    #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/9/26 10:26 # @Author : Sa.Song # @Desc ...

  2. 字符串:HDU3064-最长回文

    最长回文 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descri ...

  3. CodeForces 519E 树形DP A and B and Lecture Rooms

    给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...

  4. canvas 动画库 CreateJs 之 EaselJS(下篇)

    本文来自网易云社区 作者:田亚楠 继承 对应原文:Inheritance 我们可以继承已有的「显示对象」,创建新的自定义类.实现方法有很多种,下面介绍其中之一. 举例:实现一个继承于 Containe ...

  5. C++ 指针的小知识

    看个小例子: char* fun1(){ char * p = (char*)malloc(100); p = "helloww"; return p;} void fun2(ch ...

  6. C语言变量长度在32位和64位处理器上的关系

    C语言变量长度在32位和64位处理器上的关系       理论上来讲 我觉得数据类型的字节数应该是由CPU决定的,但是实际上主要由编译器决定(占多少位由编译器在编译期间说了算).常用数据类型对应字节数 ...

  7. Leetcode 464.我能赢吗

    我能赢吗 在 "100 game" 这个游戏中,两名玩家轮流选择从 1 到 10 的任意整数,累计整数和,先使得累计整数和达到 100 的玩家,即为胜者. 如果我们将游戏规则改为 ...

  8. HDU-1528/1962 Card Game Cheater

    两组牌中两张牌相比能赢的就连,后求最大匹配. #include <cmath> #include <cstdlib> #include <cstdio> #incl ...

  9. AIX 常用命令 第一步(uname,lspv)

    如何知道自己在运行单处理器还是多处理器内核? /unix 是指向已启动内核的符号链接.要了解正在运行什么内核模式,可输入 ls -l /unix 并查看 /unix 链接到什么文件.下面是 ls -l ...

  10. Java面试之JVM原理总结

    1.什么是JVM? 答:JVM是Java Virual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,他是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟计算机功能来实现 ...