Longest Common Subsequence
Accepted : Submit :
Time Limit : MS Memory Limit : KB Longest Common Subsequence Bobo has a sequence A=(a1,a2,…,an) of length n. He would like to know f(),f(),f() and f() where f(k) denotes the number of integer sequences X=(x1,x2,x3) where: ≤x1,x2,x3≤m;
The length of longest common subsequence of A and X is exactly k. Note: u is a subsequence of v if and only if u can be obtained by removing some of the entries from v (possibly none).
u is common subsequence of v and w if and only if u is subsequence of v and w. Input The input contains zero or more test cases and is terminated by end-of-file. For each case, The first line contains two integers n,m. The second line contains n integers a1,a2,…,an. ≤n≤
≤m,a1,a2,…,an≤
The number of tests cases does not exceed . Output For each case, output four integers which denote f(),f(),f(),f().
Sample Input Sample Output Note For the second sample, X=(,,) is the only sequence that the length of longest common subsequence of A and X is . Thus, f()=. Source
XTU OnlineJudge /**
题目:Longest Common Subsequence
链接:http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1265
题意:给定序列A,含n个数。和一个数m;
有一个X序列{x1,x2,x3},x1,x2,x3来自[1,m]区间内的数。 设:f(k)表示两个序列的最长公共子序列为k; 求序列X和A满足f(0),f(1),f(2),f(3)的X各有多少种。 思路:f(0)易求。计算出n个数中在m范围内的不同数的数量为cnt,然后计算(m-cnt)^3即可。 对n个数中<=m的数,离散化。 要求f(1),f(2),f(3);
f(3)的X序列内的数一定是n个数中获得。
f(2)的X序列内的数有两种来源,一种是三个数从n个数中获得,然后Longest Common Subsequence为2;
另一种是其中两个是n个数中获得,然后Longest Common Subsequence为2,另外一个是<=m的不包含n个数的数中获得。
f(1)的X序列内的数有三种来源,一种是3个数从n个数中获得,然后Longest Common Subsequence为1;
另一种是两个数从n个数中获得,然后Longest Common Subsequence为1;另外一个是是<=m的不包含n个数的数中获得。
另一种是其中一个是n个数中获得,然后Longest Common Subsequence为1,另外二个是<=m的不包含n个数的数中获得。 设:cnt表示n个数中<=m的数。kind表示n个数中<=m的不同数的种类。m-kind表示不包含n个数的数的种数。 枚举x1,x2,x3全是n个数组成的X序列,所有xi都枚举所有的n个数。 这些序列包含f(3),f(2)第一种来源,f(1)第一种来源的情况数。计算方法:一种是常规的Longest Common Subsequence 的dp求法,但是总时间复杂度为O(N^4);
更好的做法是:预处理next[i][j]表示i这个位置右边第一次出现j这个数的位置。那么可以O(1)判断X序列的Longest Common Subsequence为k。把它们更新到f(k)中。
flag[a[i]][a[j]][a[k]]表示这个序列可以匹配的最大LCS;取最值,最后遍历计算即可。 剩下要求f(2)的第二种来源,f(1)的第二种来源和第三种来源。
f(2)第二种来源:三种可能(x1,x2),(x2,x3),(x1,x3)位置,Longest Common Subsequence为2,另一个位置为m-kind中取。 其实情况数是一样的。f(2) += 3*Q(cnt)*(m-kind);
Q(cnt)表示cnt个数中找到与A序列的Longest Common Subsequence为2的序列数量。暴力枚举两个位置,同上面说的方法。 f(1)第二种来源:三种可能(x1,x2),(x2,x3),(x1,x3)位置,但是Longest Common Subsequence为1;另外一个位置从m-kind中取。 f(1) += 3*P(cnt)*(m-kind);
f(1)第三种来源:三种可能x1,x2,x3位置相同,另外两个位置为m-kind中取。f(1) += 3*kind*(m-kind)*(m-kind); 思考:为什么要用next[i][j]表示i这个位置右边第一次出现j这个数的位置。而不是直接next[i][j]表示j这个数是否在i的右边。
假设: 2 2
那么枚举出来2 2 2. 如果用后者所述定义,那么答案就是3了。实际上只能是2.
*/ #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> P;
const int maxn = 1e5+;
int flag[][][];
int sign[][];
int mark[];
int n, m;
int cnt , kind;
LL f[];
int a[], z;
int Next[][];
set<int> se;
void NextInit()
{
for(int i = ; i < z; i++){
for(int j = i+; j < z; j++){
if(Next[i][a[j]]==-){
Next[i][a[j]] = j;
}
}
}
}
void lisan()
{
int cnt = ;
set<int>::iterator it;
for(it = se.begin(); it!=se.end(); it++){
mark[*it] = cnt;
cnt++;
}
for(int i = ; i < z; i++){
a[i] = mark[a[i]];
}
}
void init()
{
memset(f, , sizeof f);
memset(Next, -, sizeof Next);
memset(flag, , sizeof flag);
memset(sign, , sizeof sign);
se.clear();
}
int cal(int i,int j,int k)
{
if(Next[i][a[j]]==j&&Next[j][a[k]]==k) return ;
if(Next[i][a[j]]==j) return ;
if(Next[j][a[k]]==k) return ;
if(Next[i][a[k]]==k) return ;
return ;
}
void solve()
{
kind = se.size();
cnt = z;
LL temp = m-kind;
f[] = 1LL*temp*temp*temp;
///枚举三层循环所有序列。
for(int i = ; i < z; i++){
for(int j = ; j < z; j++){
for(int k = ; k < z; k++){
int len = cal(i,j,k);
flag[a[i]][a[j]][a[k]] = max(flag[a[i]][a[j]][a[k]],len);
}
}
} se.clear();///清空原先没有离散的数据。
for(int i = ; i < z; i++){
se.insert(a[i]);
}
set<int>::iterator it, it1, it2;
for(it = se.begin(); it!=se.end(); it++){
for(it1 = se.begin(); it1!=se.end(); it1++){
for(it2 = se.begin(); it2!=se.end(); it2++){
f[flag[*it][*it1][*it2]]++;
}
} } ///枚举两层循环.
int cnt2, cnt1;
for(int i = ; i < z; i++){
for(int j = ; j < z; j++){
int len;
if(Next[i][a[j]]==j) len = ;
else len = ;
sign[a[i]][a[j]]= max(sign[a[i]][a[j]],len);
}
}
cnt1 = cnt2 = ;
for(it = se.begin(); it!=se.end(); it++){
for(it1 = se.begin(); it1!=se.end(); it1++){
if(sign[*it][*it1]==){
cnt2++;
}else cnt1++;
}
} f[] += 1LL**cnt2*(m-kind);
f[] += 1LL**cnt1*(m-kind);
///f(1)第三种来源;
f[] += 1LL**kind*(m-kind)*(m-kind);
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
z = ;
for(int i = ; i < n; i++) {
scanf("%d",&a[z]);
if(a[z]<=m) z++;
}
init();
for(int i = ; i < z; i++){
se.insert(a[i]);
}
lisan();
NextInit();
solve();
printf("%I64d %I64d %I64d %I64d\n",f[], f[], f[], f[]);
}
return ;
}

2017-5-14 湘潭市赛 Longest Common Subsequence 想法题的更多相关文章

  1. Lintcode:Longest Common Subsequence 解题报告

    Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...

  2. UVA 10405 Longest Common Subsequence (dp + LCS)

    Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...

  3. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  4. LintCode Longest Common Subsequence

    原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...

  5. [UCSD白板题] Longest Common Subsequence of Three Sequences

    Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...

  6. LCS(Longest Common Subsequence 最长公共子序列)

    最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...

  7. Longest Common Subsequence

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  8. Longest Common Subsequence & Substring & prefix

    Given two strings, find the longest common subsequence (LCS). Your code should return the length of  ...

  9. Dynamic Programming | Set 4 (Longest Common Subsequence)

    首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...

随机推荐

  1. HDU 1402 A * B Problem Plus (FFT求高精度乘法)

    A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  2. Oracle Service Bus Socket Adapter调整的参数

    之前在一个客户中做压力测试时候Oracle Service Bus性能大概达到900tps左右,和客户期望的1600tps有很大差距. 在研究了Socket Adapter的工作原理之后,判断可能是O ...

  3. insert语句太长,有StringBuilder优化一下

    private void btnSave_Click(object sender, RoutedEventArgs e) { if (IsInsert) { //假设日历控件没有选日期,那帮它赋一个当 ...

  4. 利用Teensy进行EM410x卡模拟以及暴力破解EM410X类门禁系统可行性猜想

    前些天Open入手了Teensy++2.0模拟EM410X,并且针对EM410X的门禁进行了一次暴力破解测试,以下就是相关代码以及内容. 什么是低频?什么是EM410x? 首先,我不得不再次提一下那些 ...

  5. 防止木马利用iframe框架来调用外域JS代码

    <!--防止木马利用iframe框架来调用外域JS代码,不过滤自己网站的域名的框架网页开始--><SCRIPT LANGUAGE="JavaScript"> ...

  6. 常用vim命令合集

    移动命令: h:左移 l:右移 k:上移 j:下移 ^:移动到本行第一个非空白字符上 0:移动到本行第一个字符上 gg:移动到文件头 G = shift + g:移动到文件尾 %:从一个"{ ...

  7. [转]SQL Server 性能调优(io)

      目录 诊断磁盘io问题 常见的磁盘问题 容量替代了性能 负载隔离配置有问题 分区对齐配置有问题 总结 关于io这一块,前面的东西如磁盘大小,磁盘带宽,随机读取写入,顺序读取写入,raid选择,DA ...

  8. Python——标准库 Sys模块

    ---------------------------------------------------------------------------------------------------- ...

  9. C语言-srand种子详解

    rand() 函数取得随机数的时候是通过一个叫做"种子"的变量经过计算得出一个数值, 然后得出的数值再作为新的"种子"参与下一次的运算, 这样就得到了所谓的随机 ...

  10. Windows外壳名字空间的浏览

    Windows95/98对Dos/Win3.x作了许多重大改进,在文件系统方面,它除了采用长文件名替代Dos中的8.3文件名以外,引入外壳名字空间(Shell Name Space)来代Dos文件系统 ...