UVA 11557,uva11557

來源:互聯網
上載者:User

UVA 11557,uva11557
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;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.