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)的更多相关文章

  1. UVA 11754 - Code Feat(数论)

    UVA 11754 - Code Feat 题目链接 题意:给定一个c个x, y1,y2,y3..yk形式,前s小的答案满足s % x在集合y1, y2, y3 ... yk中 思路:LRJ大白例题, ...

  2. Codeforces 1090J $kmp+hash+$二分

    题意 给出两个字符串\(s\)和\(t\),设\(S\)为\(s\)的任意一个非空前缀,\(T\)为\(t\)的任意一个非空前缀,问\(S+T\)有多少种不同的可能. Solution 看了一圈,感觉 ...

  3. 【BZOJ3940】【BZOJ3942】[Usaco2015 Feb]Censoring AC自动机/KMP/hash+栈

    [BZOJ3942][Usaco2015 Feb]Censoring Description Farmer John has purchased a subscription to Good Hoov ...

  4. UVA 11475 后缀数组/KMP

    题目链接: 题意:给定一个只含字母的字符串,求在字符串末尾添加尽量少的字符使得字符串为回文串. 思路:因为只能从末尾添加字符,所以其实求的是最长的后缀回文串.那么添加的字符为除了这个原串的最长后缀回文 ...

  5. 【POJ2185】【KMP + HASH】Milking Grid

    Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...

  6. HDU 5782 Cycle(KMP+Hash)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5782 [题目大意] 给出两个字符串,判断他们每一个前缀是否循环同构,循环同构的意思就是,字符串首位 ...

  7. Cycle (KMP + hash)

    题意:给你2个串,让你判断2个字符串的前缀是否满足首尾连接形成的环是不是一样的. 思路:我们需要提前知道的是满足条件的前缀一定满足 strA = str1 + str2, strB = str2 + ...

  8. bzoj4641 基因改造 KMP / hash

    依稀记得,$NOIP$之前的我是如此的弱小.... 完全不会$KMP$的写法,只会暴力$hash$.... 大体思路为把一个串的哈希值拆成$26$个字母的位权 即$hash(S) = \sum\lim ...

  9. UVa 11996 Jewel Magic (splay + Hash + 二分)

    题意:给定一个长度为n的01串,你的任务是依次执行如表所示的m条指令: 1 p c 在第p个字符后插入字符,p = 0表示在整个字符串之前插入2 p 删除第p个字符,后面的字符往前移3 p1 p2反转 ...

随机推荐

  1. OSSIM(开源安全信息管理系统)在企业网络管理中的应用

    国内首个Ossim技术交流群(179084574),欢迎加入我们 参与51CTO[第242期]OSSIM,企业信息安全管理利器热门技术讨论 650) this.width=650;" bor ...

  2. IDEA设置注释模板最佳实践

    效果 在方法上输入/**,然后按tab键,生成的效果如下 配置步骤 1. 在Live Templates中添加模板组,命名随意,主要是为了存放自定义的模板,方便管理 2. 在模板组下添加一个模板,具体 ...

  3. 安装oracle常见问题分析

    1,之前已经安装过软件,卸载不完全 /tmp/.oracle /tmp/ora* /var/tmp/ora* /etc/ora* 2,安装介质权限或者本身就有问题 3,oracle用户 $/HOME/ ...

  4. Yeslab 华为安全HCIE-第七门-Agile Controlle

    课程目录:   华为安全HCIE-第七门-Agile Controller(12篇)\1_aglie_controller产品亮点讲解.avi 华为安全HCIE-第七门-Agile Controlle ...

  5. XTUOJ 1206 Dormitory's Elevator

    Dormitory's Elevator Time Limit : 1000 MS   Memory Limit : 65536 KB Problem Description The new dorm ...

  6. [Parcel] Bundle a React App with Parcel

    Parcel comes in as the new cool kid in the bundlers world. Unlike other bundlers which take lots of ...

  7. SPSS提示“列表中不同意存在字符串变量”的解决方法

    今天用SPSS对一些数据进行主成分分析,SPSS 19.0进行主成分分析的方法是:分析--降维--因子分析,可是当导入一些变量的时候.就会弹出窗体说"列表中不同意存在字符串变量", ...

  8. JavaScript数组的某些操作(二)

    7.颠倒数组中元素的顺序(注意:不是为数组排序)--reverse方法 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...

  9. js --- 事件冒泡 事件捕获

    先上结论: 他们是描述事件触发时序问题的术语.事件捕获指的是从document到触发事件的那个节点,即自上而下的去触发事件.相反的,事件冒泡是自下而上的去触发事件.绑定事件方法的第三个参数,就是控制事 ...

  10. javafx KeyCombination

    import javafx.application.Application; import javafx.application.Platform; import javafx.event.Actio ...