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反转 ...
随机推荐
- poj--1237--Drainage Ditches(最大流)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Sub ...
- Linux下大型容量件的切割与合并
Linux下大型容量件的切割与合并 当前目录下有sp4.exe文件容量129M我用64的U盘把spe.exe考到另一台机器上#split -b 60m sp4.exe-b, --bytes=SIZE ...
- vue-router学习例子分享
http://blog.csdn.net/bboyjoe/article/details/52804988 <!DOCTYPE html> <html lang="en&q ...
- nginx 实现跨域
nginx 添加头部跨域. location / { add_header 'Access-Control-Allow-Origin' '*'; //允许的域 add_header 'Access-C ...
- 【TC SRM 718 DIV 2 B】Reconstruct Graph
[Link]: [Description] 给你两个括号序列; 让你把这两个括号序列合并起来 (得按顺序合并) 使得组成的新的序列为合法序列; 即每个括号都能匹配; 问有多少种合并的方法; [Solu ...
- [转]C语言字节对齐问题详解
C语言字节对齐问题详解 转载:https://www.cnblogs.com/clover-toeic/p/3853132.html 引言 考虑下面的结构体定义: typedef struct{ ch ...
- Java解析注解
package com.itbuluoge.anno; import java.lang.reflect.Method; import java.util.ArrayList; import java ...
- 如何让hudson的两个job共用一个svn工作目录
作者:朱金灿 来源:http://blog.csdn.net/clever101 现在我的需求是这样的:一个软件需要编译完全版本和基础版本,完全版本的基础功能较多,基础版本只包含了基础功能.有时只需要 ...
- Codefroces Educational Round 26 837 C. Two Seals
C. Two Seals time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- react-native React Native version mismatch
android/app/build.gradle file: dependencies { compile fileTree(dir: "libs", include: [ ...