Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define int long long
- #define pb push_back
- #define all(a) a.begin(), a.end()
- #define rall(a) a.rbegin(), a.rend()
- void out(vector<int> a) {
- for (auto it = a.begin(); it != a.end(); ++it) cout << (*it) << " ";
- cout << "\n";
- }
- int n, m;
- string s, t;
- int sz;
- const int b = 5, mod = 1354828, mod2 = mod * mod;
- vector<int> power;
- vector<int> hash1, hash2;
- inline void read() {
- cin >> n >> m >> s >> t;
- sz = max(n, m);
- power.resize(sz + 1);
- hash1.resize(sz + 1);
- hash2.resize(sz + 1);
- }
- int trans(char c) {
- if (c == 'A') return 1;
- if (c == 'C') return 2;
- if (c == 'G') return 3;
- if (c == 'T') return 4;
- }
- void calc_power() {
- power[0] = 1;
- for (int i = 1; i <= sz; ++i)
- power[i] = (power[i-1] * b) % mod;
- }
- void calc_hash(vector<int> &h, string &str) {
- h[0] = trans(str[0]);
- for (int i = 1; i < str.size(); ++i) {
- int c = trans(str[i]);
- h[i] = (h[i - 1] * b + c) % mod;
- }
- }
- int get_hash(vector<int> &h, int l, int r) {
- if (l == 0) return h[r];
- else return ((mod2 + h[r] - (h[l-1] * power[r-l+1] % mod))+ mod) % mod;
- }
- bool used[mod];
- bool f(int mid) {
- memset(used, false, sizeof(used));
- int cnt = 0;
- for (int i = 0; i <= n - mid; ++i) {
- int id = get_hash(hash1, i, i + mid - 1);
- if (!used[id]) ++cnt;
- used[id] = true;
- }
- for (int i = 0; i <= m - mid; ++i) {
- int id = get_hash(hash2, i, i + mid - 1);
- if (used[id]) --cnt;
- used[id] = false;
- }
- if (cnt == 0) return true;
- else return false;
- }
- string res(int k) {
- memset(used, false, sizeof(used));
- for (int i = 0; i <= m - k; ++i) {
- int id = get_hash(hash2, i, i + k - 1);
- used[id] = true;
- }
- for (int i = 0; i <= n - k; ++i) {
- int id = get_hash(hash1, i, i + k - 1);
- if (!used[id]) return s.substr(i, k);
- }
- }
- vector<string> ans;
- inline void solve() {
- calc_power();
- calc_hash(hash1, s);
- calc_hash(hash2, t);
- int l = 1, r = min(n, m) + 1;
- for (int i = 0; i<20; ++i) {
- int mid = (l + r) / 2;
- if (f(mid)) l = mid + 1;
- else r = mid;
- }
- if (r == min(n, m) + 1) {
- if (n > m) cout << s.substr(0, m+1);
- else cout << "NONE";
- }
- else cout << res(r);
- }
- signed main() {
- ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
- cout << fixed << setprecision(20);
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- read();
- solve();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement