URAL 1517Freedom of Choice

来源:互联网 发布:按键精灵修改mac 编辑:程序博客网 时间:2024/05/17 08:39

Description

Background

Before Albanian people could bear with the freedom of speech (this story is fully described in the problem "Freedom of speech"), another freedom - the freedom of choice - came down on them. In the near future, the inhabitants will have to face the first democratic Presidential election in the history of their country.
Outstanding Albanian politicians liberal Mohammed Tahir-ogly and his old rival conservative Ahmed Kasym-bey declared their intention to compete for the high post.

Problem

According to democratic traditions, both candidates entertain with digging dirt upon each other to the cheers of their voters' approval. When occasion offers, each candidate makes an election speech, which is devoted to blaming his opponent for corruption, disrespect for the elders and terrorism affiliation. As a result the speeches of Mohammed and Ahmed have become nearly the same, and now it does not matter for the voters for whom to vote.
The third candidate, a chairman of Albanian socialist party comrade Ktulhu wants to make use of this situation. He has been lazy to write his own election speech, but noticed, that some fragments of the speeches of Mr. Tahir-ogly and Mr. Kasym-bey are completely identical. Then Mr. Ktulhu decided to take the longest identical fragment and use it as his election speech.

Input

The first line contains the integer number N (1 ≤ N ≤ 100000). The second line contains the speech of Mr. Tahir-ogly. The third line contains the speech of Mr. Kasym-bey. Each speech consists of N capital latin letters.

Output

You should output the speech of Mr. Ktulhu. If the problem has several solutions, you should output any of them.

Sample Input

inputoutput

28VOTEFORTHEGREATALBANIAFORYOUCHOOSETHEGREATALBANIANFUTURE
THEGREATALBANIA

后缀数组+二分

#include<set>#include<map>#include<ctime>#include<cmath>#include<stack>#include<queue>#include<bitset>#include<cstdio>#include<string>#include<cstring>#include<iostream>#include<algorithm>#include<functional>#define rep(i,j,k) for (int i = j; i <= k; i++)#define per(i,j,k) for (int i = j; i >= k; i--)#define lson x << 1, l, mid#define rson x << 1 | 1, mid + 1, r#define fi first#define se second#define mp(i,j) make_pair(i,j)#define pii pair<int,int>using namespace std;typedef long long LL;const int low(int x) { return x&-x; }const double eps = 1e-8;const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 2e5 + 10;const int read(){char ch = getchar();while (ch<'0' || ch>'9') ch = getchar();int x = ch - '0';while ((ch = getchar()) >= '0'&&ch <= '9') x = x * 10 + ch - '0';return x;}int T = 0;struct Sa{char s[N];int rk[2][N], sa[N], h[N], w[N], now, n;int rmq[N][20], lg[N], bel[N];bool GetS(){if (scanf("%d", &n) == -1) return false;scanf("%s", s + 1);for (n = 1; s[n]; n++) bel[n] = 0;bel[n++] = 2;scanf("%s", s + n);while (s[n]) bel[n++] = 1;--n;return true;}void getsa(int z, int &m){int x = now, y = now ^= 1;rep(i, 1, z) rk[y][i] = n - i + 1;for (int i = 1, j = z; i <= n; i++)if (sa[i] > z) rk[y][++j] = sa[i] - z;rep(i, 1, m) w[i] = 0;rep(i, 1, n) w[rk[x][rk[y][i]]]++;rep(i, 1, m) w[i] += w[i - 1];per(i, n, 1) sa[w[rk[x][rk[y][i]]]--] = rk[y][i];for (int i = m = 1; i <= n; i++){int *a = rk[x] + sa[i], *b = rk[x] + sa[i - 1];rk[y][sa[i]] = *a == *b&&*(a + z) == *(b + z) ? m - 1 : m++;}}void getsa(int m){//n = strlen(s + 1);rk[1][0] = now = sa[0] = s[0] = 0;rep(i, 1, m) w[i] = 0;rep(i, 1, n) w[s[i]]++;rep(i, 1, m) rk[1][i] = rk[1][i - 1] + (bool)w[i];rep(i, 1, m) w[i] += w[i - 1];rep(i, 1, n) rk[0][i] = rk[1][s[i]];rep(i, 1, n) sa[w[s[i]]--] = i;rk[1][n + 1] = rk[0][n + 1] = 0;//多组的时候容易出bugfor (int x = 1, y = rk[1][m]; x <= n && y <= n; x <<= 1) getsa(x, y);for (int i = 1, j = 0; i <= n; h[rk[now][i++]] = j ? j-- : j){if (rk[now][i] == 1) continue;int k = n - max(sa[rk[now][i] - 1], i);while (j <= k && s[sa[rk[now][i] - 1] + j] == s[i + j]) ++j;}}void getrmq(){h[n + 1] = h[1] = lg[1] = 0;rep(i, 2, n) rmq[i][0] = h[i], lg[i] = lg[i >> 1] + 1;for (int i = 1; (1 << i) <= n; i++){rep(j, 2, n){if (j + (1 << i) > n + 1) break;rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << i - 1)][i - 1]);}}}int lcp(int x, int y){int l = min(rk[now][x], rk[now][y]) + 1, r = max(rk[now][x], rk[now][y]);return min(rmq[l][lg[r - l + 1]], rmq[r - (1 << lg[r - l + 1]) + 1][lg[r - l + 1]]);}bool check(int x){int f[3] = { 0,0,0 };rep(i, 2, n){if (h[i] >= x) f[bel[sa[i]]] = f[bel[sa[i - 1]]] = 1;else{if (f[0] && f[1]) return true;f[0] = f[1] = 0;}}return f[0] && f[1];}void putout(int x){int f[3] = { 0,0,0 };rep(i, 2, n){if (h[i] >= x) f[bel[sa[i]]] = f[bel[sa[i - 1]]] = 1;else{if (f[0] && f[1]){rep(j, 1, x) printf("%c", s[sa[i - 1] + j - 1]);putchar(10);return;}f[0] = f[1] = 0;}}rep(j, 1, x) printf("%c", s[sa[n] + j - 1]);putchar(10);return;}void work(){getsa(300);int l = 1, r = n;while (l <= r){if (check(l + r >> 1)) l = (l + r >> 1) + 1;else r = (l + r >> 1) - 1;}putout(r);}}sa;int main(){while (sa.GetS()) sa.work();return 0;}

0 0
原创粉丝点击