UVA 11557 - Code Theft (KMP + HASH)
UVA 11557 - Code Theft
题意:给定一些代码文本。然后在给定一个现有文本,找出这个现有文本和前面代码文本,反复连续行最多的这些文本
思路:把每一行hash成一个值。然后对于每个文本计算最大匹配值,枚举后缀。然后利用KMP去找就可以
代码:
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <iostream>
#include <vector>
using namespace std; typedef unsigned long long ull; const ull X = 123;
const int N = 105; int n, next[1000005];
string name[N];
string s;
vector<int> ans;
vector<ull> code[N]; void hash(string s, int u) {
string ss = "";
int l = 0, r = s.length() - 1, len = s.length();;
while (s[l] == ' ' && l < len) l++;
while (s[r] == ' ' && r >= 0) r--;
for (int i = l; i <= r; i++) {
ss += s[i];
while (s[i] == ' ' && s[i + 1] == ' ' && i < r) i++;
}
if (ss == "") return;
ull ans = 0;
for (int i = ss.length() - 1; i >= 0; i--)
ans = ans * X + ss[i];
code[u].push_back(ans);
} void build(int i) {
code[i].clear();
while (getline(cin, s) && s != "***END***") {
hash(s, i);
}
} vector<ull> T; void getnext() {
int N = T.size();
next[0] = next[1] = 0;
int j = 0;
for (int i = 2 ; i <= N; i++) {
while (j && T[i - 1] != T[j]) j = next[j];
if (T[i - 1] == T[j]) j++;
next[i] = j;
}
} int find() {
int ans = 0;
int N = code[n].size(), m = T.size(), j = 0;
for (int i = 0; i < N; i++) {
while (j && code[n][i] != T[j]) j = next[j];
if (code[n][i] == T[j]) j++;
ans = max(ans, j);
if (j == m)
return m;
}
return ans;
} int cal(int u) {
int ans = 0;
int sz1 = code[u].size();
for (int i = 0; i < sz1; i++) {
T.clear();
for (int j = i; j < sz1; j++)
T.push_back(code[u][j]);
getnext();
ans = max(ans, find());
}
return ans;
} void solve() {
int Max = 0;
ans.clear();
for (int i = 0; i < n; i++) {
int tmp = cal(i);
if (tmp > Max) {
Max = tmp;
ans.clear();
ans.push_back(i);
}
else if (tmp == Max) ans.push_back(i);
}
cout << Max;
if (Max) {
for (int i = 0; i < ans.size(); i++)
cout << " " << name[ans[i]];
}
cout << endl;
} int main() {
while (cin >> n) {
getchar();
for (int i = 0; i < n; i++) {
getline(cin, name[i]);
build(i);
}
build(n);
solve();
}
return 0;
}
UVA 11557 - Code Theft (KMP + HASH)的更多相关文章
- UVA 11754 - Code Feat(数论)
UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...
- Codeforces 1090J $kmp+hash+$二分
题意 给出两个字符串\(s\)和\(t\),设\(S\)为\(s\)的任意一个非空前缀,\(T\)为\(t\)的任意一个非空前缀,问\(S+T\)有多少种不同的可能. Solution 看了一圈,感觉 ...
- 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈
[BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...
- UVA 11475 后缀数组/KMP
题目链接: 题意:给定一个只含字母的字符串,求在字符串末尾添加尽量少的字符使得字符串为回文串. 思路:因为只能从末尾添加字符,所以其实求的是最长的后缀回文串.那么添加的字符为除了这个原串的最长后缀回文 ...
- 【POJ2185】【KMP + HASH】Milking Grid
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- HDU 5782 Cycle(KMP+Hash)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5782 [题目大意] 给出两个字符串,判断他们每一个前缀是否循环同构,循环同构的意思就是,字符串首位 ...
- Cycle (KMP + hash)
题意:给你2个串,让你判断2个字符串的前缀是否满足首尾连接形成的环是不是一样的. 思路:我们需要提前知道的是满足条件的前缀一定满足 strA = str1 + str2, strB = str2 + ...
- bzoj4641 基因改造 KMP / hash
依稀记得,$NOIP$之前的我是如此的弱小.... 完全不会$KMP$的写法,只会暴力$hash$.... 大体思路为把一个串的哈希值拆成$26$个字母的位权 即$hash(S) = \sum\lim ...
- UVa 11996 Jewel Magic (splay + Hash + 二分)
题意:给定一个长度为n的01串,你的任务是依次执行如表所示的m条指令: 1 p c 在第p个字符后插入字符,p = 0表示在整个字符串之前插入2 p 删除第p个字符,后面的字符往前移3 p1 p2反转 ...
随机推荐
- JUnit中assertEquals和assertSame方法的不同
1)提供的接口数量不完全相同. assertEquals支持boolean,long,int等等java primitiveType变量.assertSame只支持Object. 2)比较的逻辑不同, ...
- beego orm commonDAL通用方法汇总
orm 通用方法——QueryModelById 主键查询 orm 通用方法——GetOneModel 条件查询一个对象 orm 通用方法——QueryModelCount条件查询记录数 orm 通用 ...
- BZOJ 3781 莫队
思路:不能再裸的裸题-- //By SiriusRen #include <cmath> #include <cstdio> #include <algorithm> ...
- VS初始化设置
来源于网上整理和 书<aps.net mvc企业级实战>中. 1.vs模版 版权注释信息 1.我的电脑上VS2015安装在D盘中,所以找的目录为:D:\Program Files (x86 ...
- Chromium Graphics : GPU Accelerated Compositing in Chrome
GPU Accelerated Compositing in Chrome Tom Wiltzius, Vangelis Kokkevis & the Chrome Graphics team ...
- 【Docker基本操作】
关于镜像的操作 docker search centos 搜索所有centos的镜像 docker pull centos 获取centos镜像 docker images 或 docker imag ...
- 測试password强度
<html> <!--激情在最后面.请看最后面红色字 这是是个计算password强度的实例 网上有非常多这种样例 只是呢,都不怎么好 这是我写的一个完整的效果,能够通用, new一 ...
- Android学习笔记(三)
ContentProvider简单介绍 ContentProvider是不同应用程序之间进行数据交换的标准API,当一个应用程序须要把自己的数据暴露给其它程序使用时.该应用程序便可通过提供Conten ...
- jQuery对表格进行类样式
<%-- <%@ page language="java" contentType="text/html; charset=utf-8" pageE ...
- Docker+Jenkins持续集成
Docker+Jenkins持续集成 使用etcd+confd实现容器服务注册与发现 前面我们已经通过jenkins+docker搭建了基本的持续集成环境,实现了服务的自动构建和部署,但是,我们遇 ...