题目大意

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_JUDGEfreopen("XSY2336.in", "r", stdin);#endifusing 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));
}

转载于:https://www.cnblogs.com/ZeonfaiHo/p/7112157.html

Codeforces 471 D MUH and Cube Walls相关推荐

  1. codeforces D MUH and Cube Walls(kmp)

    先分别计算a,b数组的差分,得到两个数组,文本数组和模式数组,然后使用kmp统计模式数组在文本数组出现个数.对于b数组长度为1时,结果就是数组a的长度. 代码参考: OJ/codeforces/471 ...

  2. 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串中出现过!最后输出 ...

  3. Codeforces #471

    C(分段) 题意: 分析: 我们分别考虑p=2和p>=3的情况 当p=2的时候,个数明显是[L,R]内完全平方数的个数 当p>=3的时候,我们注意到这样的数字个数是1e6级别的,且a最多也 ...

  4. 【CodeForces - 471B】MUH and Important Things (模拟,细节)

    题干: It's time polar bears Menshykov and Uslada from the zoo of St. Petersburg and elephant Horace fr ...

  5. 【CodeForces - 471C】MUH and House of Cards (思维,找规律)

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

  6. 前缀函数及kmp算法

    1.字符串基础 1.1 字符集 一个字符集是一个建立了全序关系的集合,也就是说中的任意两个不两只的元素和都可以比较大小,要么,要么.字符集中的元素称为字符. 1.2 字符串 一个字符串S是将n个字符顺 ...

  7. linux mint 下载迅雷安装包,Linux Mint如何安装“微信、QQ、迅雷、WPS办公软件”等国内上瘾软件...

    Ubuntu14-04 MySQL-5.6.21通用二进制安装 #卸载mysql /etc/init.d/mysqld stop &> /dev/null killall mysqld ...

  8. Prefix function. Knuth–Morris–Pratt algorithm

    Prefix function. Knuth–Morris–Pratt algorithm Prefix function definition You are given a string \(s\ ...

  9. 算法笔记--KMP算法 EXKMP算法

    1.KMP算法 这个博客写的不错:http://www.cnblogs.com/SYCstudio/p/7194315.html 模板: next数组的求解,那个循环本质就是如果相同前后缀不能加上该位 ...

最新文章

  1. 拜访了这位小哥的GitHub后,我失眠了!
  2. 做科研发论文一直找不到研究热点?硕博导师都在关注的平台你还不快来?
  3. Windows Server 2008 多元密码策略之ADSIEDIT
  4. php绘图技术加水印,PHP图片添加水印功能示例小结
  5. 全球及中国手持式无线电台行业十四五发展动态及前景趋势调研报告2022-2027年
  6. [Leetcode] Sqrt(x)
  7. kotlin读取html,kotlin 使用skrape {it}从html获取数据 - 糯米PHP
  8. 疯狂游戏型计算机配置清单,电脑配置清单
  9. Zookeeper--Watcher机制源码剖析一
  10. 反应堆模式(reactor)
  11. 去哪儿网2018春招软件开发工程师、前段开发工程师编程题 - 题解
  12. C11中gets()的函数被删除
  13. 异常处理汇总-数据库系列
  14. 轻松搭建Windows8云平台开发环境
  15. java netbeans教程_Netbeans下载安装教程教程
  16. 十分钟掌握Nodejs下载和安装
  17. ipad下载python_ipad python
  18. web前端 vue 面试题(一)
  19. win10的linux内核版本,微软决定在Windows10中发布一个完整的Linux内核
  20. 禾穗HERS | 听说妳事业成功都是靠“关系”?

热门文章

  1. JSON与XML的选择
  2. 我的YUV播放器MFC小笔记:unicode编码、宽字符
  3. Mybatis if 判断等于一个字符串
  4. Redis--Windos下的安装和使用
  5. 订餐系统jsp模板_java|web|jsp网上订餐系统|餐饮管理|在线点餐外卖网站|源码代码...
  6. 【java】序列化:ProtoBuf 与 JSON 的比较
  7. 【clickhouse】clickhouse 利用Grafana与系统表监控ClickHouse查询
  8. 【MySQL】MySQL 中的函数
  9. IllegalStateException: Error reading delta file hdfs://xxx/spark/xx/state/0/11/1.delta
  10. maven的pom.xml文件