2017-5-14 湘潭市赛 Longest Common Subsequence 想法题
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 想法题的更多相关文章
- Lintcode:Longest Common Subsequence 解题报告
Longest Common Subsequence 原题链接:http://lintcode.com/zh-cn/problem/longest-common-subsequence/ Given ...
- UVA 10405 Longest Common Subsequence (dp + LCS)
Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, pri ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode Longest Common Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-common-subsequence/ 题目: Given two strings, find t ...
- [UCSD白板题] Longest Common Subsequence of Three Sequences
Problem Introduction In this problem, your goal is to compute the length of a longest common subsequ ...
- LCS(Longest Common Subsequence 最长公共子序列)
最长公共子序列 英文缩写为LCS(Longest Common Subsequence).其定义是,一个序列 S ,如果分别是两个或多个已知序列的子序列,且是所有符合此条件序列中最长的,则 S 称为已 ...
- Longest Common Subsequence
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Longest Common Subsequence & Substring & prefix
Given two strings, find the longest common subsequence (LCS). Your code should return the length of ...
- Dynamic Programming | Set 4 (Longest Common Subsequence)
首先来看什么是最长公共子序列:给定两个序列,找到两个序列中均存在的最长公共子序列的长度.子序列需要以相关的顺序呈现,但不必连续.例如,"abc", "abg", ...
随机推荐
- js 的 slice方法
slice() 方法可从已有的数组中返回选定的元素. string.slice( beginslice [, endSlice] ); 下面是参数的详细信息: beginSlice : 从零开始的索引 ...
- [Python爬虫] 之二十三:Selenium +phantomjs 利用 pyquery抓取智能电视网数据
一.介绍 本例子用Selenium +phantomjs爬取智能电视网(http://news.znds.com/article/news/)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字 ...
- 利用JS实现vue中的双向绑定
Vue 已经是主流框架了 它的好处也不用多说,都已经是大家公认的了 那我们就来理解一下Vue的单向数据绑定和双向数据绑定 然后再使用JS来实现Vue的双向数据绑定 单向数据绑定 指的是我们先把模板写好 ...
- vue总结介绍
转自(https://zhuanlan.zhihu.com/p/23078117) 模板语法 Vue 提供了一堆数据绑定语法. {{ text }} 文本插值 <div v-html=" ...
- HDU2491 Priest John's Busiest Day
题目链接 题意: 有n个人要进行乒乓球比赛,每一个人都一个能力值.每一个人出现的次序就是他们住的位置 如今要求进行一场比赛,三个人,裁判的能力值在两个选手之间,住的位置也在两个人的之间 问这样的比赛一 ...
- HTML5 Canvas 绘制库存变化折线 画入库出库柱状图
代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...
- struts2入门示例(hello world)
1. 环境搭建 按照之前的文章配置好myeclipse的jdk和tomcat,并新建一个web项目后,可开始动手配置与struts2相关的地方了.首先去struts的官网下载好最新的struts2代码 ...
- (三)Maven基本概念——常用插件的配置
看注释———— pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- linux socket读数据错误解释
EINTR 表示某种阻塞的操作,被接收到的信号中断,造成的一种错误返回值. EAGAIN 从字面上来看,是提示再试一次.这个错误经常出现在当应用程序进行一些非阻塞(non-blocking)操作( ...
- 灾备用Oracle语句备查
Oracle常用Sql . 查看数据库的归档模式及闪回是否启用 select log_mode,open_mode,flashback_on .查看实例状态 sel ...