URAL1517-Freedom of Choice
来源:互联网 发布:mysql5.7数据库安装 编辑:程序博客网 时间:2024/05/17 08:21
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
题意:给你两个字符串,求出它们的最长公共子串
解题思路:后缀数组,将两个字符串连接起来,中间用没出现过的字符连接即可
#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>#include <stack>#include <queue>#include <vector>#include <map>using namespace std;#define N 200010struct Sa{ char s[N],s1[N],s2[N]; int rk[2][N],sa[N],h[N],w[N],now,n; int rmq[N][20],lg[N]; bool GetS() { if(scanf("%d",&n)==EOF) return false; scanf("%s",s1+1); scanf("%s",s2+1); for(int i=1;i<=n;i++) s[i]=s1[i]; s[n+1]='$'; for(int i=n+2;i<=n+n+2;i++) s[i]=s2[i-n-1]; n=n+n+1; return true; } void getsa(int z,int &m) { int x=now,y=now^=1; for(int i=1;i<=z;i++) 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; for(int i=1;i<=m;i++) w[i]=0; for(int i=1;i<=n;i++) w[rk[x][rk[y][i]]]++; for(int i=1;i<=m;i++) w[i]+=w[i-1]; for(int i=n;i>=1;i--) 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) { now=rk[1][0]=sa[0]=s[0]=0; for(int i=1;i<=m;i++) w[i]=0; for(int i=1;i<=n;i++) w[s[i]]++; for(int i=1;i<=m;i++) rk[1][i]=rk[1][i-1]+(bool)w[i]; for(int i=1;i<=m;i++) w[i]+=w[i-1]; for(int i=1;i<=n;i++) rk[0][i]=rk[1][s[i]]; for(int i=1;i<=n;i++) sa[w[s[i]]--]=i; rk[1][n+1]=rk[0][n+1]=0; for(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; for(int i=2;i<=n;i++) rmq[i][0]=h[i],lg[i]=lg[i>>1]+1; for(int i=1;(1<<i)<=n;i++) { for(int j=2;j<=n;j++) { 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]]); } void work() { int ans=-1; int mi; for(int i=2;i<=n;i++) { int MAX=max(sa[i-1],sa[i]); int MIN=min(sa[i-1],sa[i]); if(MAX>n/2&&MIN<n/2&&ans<h[i]) ans=h[i],mi=MIN; } for(int i=mi;i<mi+ans;i++) printf("%c",s[i]); printf("\n"); }}sa;int main(){ while(sa.GetS()) { sa.getsa(300); sa.work(); } return 0;}
阅读全文
0 0
- URAL1517-Freedom of Choice
- Ural1517 Freedom of choice, 后缀数组,最长公共子串
- Freedom of Choice URAL
- URAL 1517 Freedom of Choice
- URAL 1517 Freedom of Choice
- URAL 1517Freedom of Choice
- timus 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 (后缀数组 输出两个串最长公共子串)
- POJ 2774 Long Long Message && URAL 1517. Freedom of Choice(求最长重复子序列)
- URAL 题目1517. Freedom of Choice(后缀数组,求公共最长串)
- URAL 1517. Freedom of choice(后缀数组:最长公共连续子串)
- ural1517
- Freedom of the Park
- Degrees of freedom
- IOS-React-Native:No bundle URL present
- C++面试题_01
- easypermissions
- Unicode和UTF-8
- Java并发多线程面试题 Top 50
- URAL1517-Freedom of Choice
- CodeForces
- 大端小端问题
- 17. Letter Combinations of a Phone Number
- 网络爬虫规则(一)
- 15.验证码
- Qt 4.8.7 -> msys2+mingw64 静态编译
- DrawerLayout,TabLayout和xlistview网络请求数据(类似今日头条)
- Mysql 学习之EXPLAIN作用