Ural 1517. Freedom of Choice 后缀数组
来源:互联网 发布:淘宝沙河力度 编辑:程序博客网 时间:2024/05/16 17:03
1517. Freedom of Choice
Time limit: 2.0 second
Memory limit: 64 MB
Memory limit: 64 MB
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
28VOTEFORTHEGREATALBANIAFORYOUCHOOSETHEGREATALBANIANFUTURE
THEGREATALBANIA
Problem Author: Ilya Grebnov, Nikita Rybak, Dmitry Kovalioff
Problem Source: Timus Top Coders: Third Challenge
Problem Source: Timus Top Coders: Third Challenge
/** 后缀数组 * * 求两个串的公共子串,即所有后缀的最长公共子串。 * 首先把两个字符串连接,然后求出height[i]值,需要判断suffix[sa[i-1]]和suffix[sa[i]]是否来自不同的字符串。 * 具体详见IOI2009罗穗骞的论文 *//**< */#include <stdio.h>#include <string.h>int const MAXN = 200050;int wa[MAXN], wb[MAXN], ws[MAXN], wv[MAXN];int rank[MAXN], sa[MAXN], height[MAXN], r[MAXN], n, k;int cmp( int* r, int a, int b, int L ){return r[a]== r[b] && r[a+ L]== r[b+ L];}void da( int* r, int* sa, int n, int m ){int i, j, p, *x= wa, *y= wb, *t;for( i= 0; i< m; ++i ) ws[i]= 0; for( i= 0; i< n; ++i ) ws[ x[i]= r[i] ]++;for( i= 1; i< m; ++i ) ws[i]+= ws[i-1];for( i= n- 1; i>= 0; i-- ) sa[ --ws[ x[i] ] ]= i;for( j= 1, p= 1; p< n; j*= 2, m= p ){for( p= 0, i= n- j; i< n; ++i ) y[p++]= i; for( i= 0; i< n; ++i ) if( sa[i]>= j ) y[p++]= sa[i]- j;for( i= 0; i< n; ++i ) wv[i]= x[y[i]];for( i= 0; i< m; ++i ) ws[i]= 0;for( i= 0; i< n; ++i ) ws[ wv[i] ]++; for( i= 1; i< m; ++i ) ws[i]+= ws[i-1];for( i= n- 1; i>= 0; i-- ) sa[ --ws[ wv[i] ] ]= y[i];t= x, x= y, y= t, p= 1; x[ sa[0] ]= 0;for( i= 1; i< n; ++i )x[ sa[i] ]= cmp( y, sa[i-1], sa[i], j )? p- 1: p++; }}void callheight( int* r, int*sa, int n ){int i, j, k= 0;for( i= 1; i<= n; ++i ) rank[ sa[i] ]= i;for( i= 0; i< n; height[ rank[i++] ]= k )for( k?k--:0, j= sa[ rank[i]- 1]; r[i+k]== r[j+k]; k++ ); }int check(int t, int len1)//检测计算的height是否来自不同的字符串{if(sa[t] > len1 && sa[t - 1] > len1) return 0;if(sa[t] < len1 && sa[t - 1] < len1) return 0;return 1;}char s[200010];int main(){int i, len, st;scanf("%d", &len);scanf("%s", s);s[len] = '#';scanf("%s", s+len+1);n = len+len+1;for(i = 0; i < n; i ++) r[i] = (int)s[i];da(r, sa, n + 1, 10000);callheight( r, sa, n );int max = -1;for(i = 1; i <= n; i ++){if(check(i, len) && height[i] > max){max = height[i];st = i;}}for(i = sa[st]; i < sa[st]+max; i ++) printf("%c", s[i]);printf("\n");return 0;}
- Ural 1517. Freedom of Choice 后缀数组
- URAL 1517 Freedom of Choice 后缀数组
- URAL 1517 Freedom of Choice 后缀数组 入门
- URAL 1517. Freedom of choice(后缀数组:最长公共连续子串)
- URAL 题目1517. Freedom of Choice(后缀数组,求公共最长串)
- URAL 1517. Freedom of choice(后缀数组:最长公共连续子串)
- Freedom of Choice URAL
- timus 1517 Freedom of Choice//后缀数组
- URAL 1517 Freedom of Choice (后缀数组 输出两个串最长公共子串)
- URAL 1517 Freedom of Choice
- URAL 1517 Freedom of Choice
- URAL 1517Freedom of Choice
- Ural1517 Freedom of choice, 后缀数组,最长公共子串
- POJ 2774 Long Long Message && URAL 1517. Freedom of Choice(求最长重复子序列)
- URAL1517-Freedom of Choice
- ural 1297 Palindrome 后缀数组
- ural 1297 Palindrome(后缀数组)
- 【后缀数组】【URAL 1297】Palindrome
- Javascript 的addEventListener()及attachEvent()区别分析
- 如何使用java中的Properties
- 【rzxt】_笔者支招:六步骤清除开始菜单运行命令
- 最简单的小灯闪烁
- 1.3 抽象数据类型表示和实现
- Ural 1517. Freedom of Choice 后缀数组
- 51下广告灯数码管和MSP430数码管
- linux下解压*.tar.xz文件
- POJ 1151: Atlantis
- c语言字符格式
- Weed (DFS)
- MySql支持的运算符
- 51/MSP430数码管的拉幕式效果
- java几种读取.properties文件的方法