xtu-1265 Longest Common Subsequence(字符串/计数)
来源:互联网 发布:点卡商城源码 编辑:程序博客网 时间:2024/05/18 22:43
Longest Common Subsequence
Accepted : 11 Submit : 105Time Limit : 3000 MS Memory Limit : 65536 KBLongest Common Subsequence
Bobo has a sequence
1≤x 1 ,x 2 ,x 3 ≤m ;- The length of longest common subsequence of
A andX is exactlyk .
Note:
u is a subsequence ofv if and only ifu can be obtained by removing some of the entries fromv (possibly none).u is common subsequence ofv andw if and only ifu is subsequence ofv andw .
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
1≤n≤200 1≤m,a 1 ,a 2 ,…,a n ≤10 6 - The number of tests cases does not exceed
10 .
Output
For each case, output four integers which denote
Sample Input
3 31 2 25 31 2 3 2 1
Sample Output
1 14 11 10 1 17 9
Note
For the second sample,
A中只要ai>m则舍去,因为X取不到ai。设A中一共有cnt种元素,那么还剩m-cnt个元素没有。
枚举x1,x2,x3的值∈[1,cnt+1],其中cnt+1表示x取A中不存在的值。预处理出nxt[i][c],表示第i个元素后,元素c是出现在第几个位置。
枚举集合(x1,x2,x3)的子集,找出是A的子序列的最大子集,如果xi不在这个子集中且xi的取值为cnt+1,那么方案数*(m-cnt)
#include<bits/stdc++.h>using namespace std;typedef long long LL;const int MX = 1e6 + 5;int vis[MX], a[205], sz, cnt;int nxt[205][205], t[5], st[5];LL f[5];int solve(int len) { if(len == 0) return 1; int i = 0, index; for(index = nxt[0][st[i]]; index > 0 && i < len;) { index = nxt[index][st[++i]]; } if(i==len) return 1; return 0;}int S, mx;void dfs(int len, int i, int sta) { if(i > 2) { if(solve(len) && mx < len) { S = sta; mx = len; } } else { dfs(len, i + 1, sta); st[len] = t[i]; dfs(len + 1, i + 1, sta | (1 << i)); }}int main() { int n, m; while(~scanf("%d%d", &n, &m)) { memset(vis, 0, sizeof(vis)); memset(nxt, -1, sizeof(nxt)); memset(f, 0, sizeof(f)); for(int i = 1; i <= n; i++) scanf("%d", &a[i]); sz = cnt = 0; for(int i = 1; i <= n; i++) { if(a[i] <= m) { a[++sz] = a[i]; if(vis[a[sz]]) a[sz] = vis[a[sz]]; else { vis[a[sz]] = ++cnt; a[sz] = vis[a[sz]]; } } } for(int i = 0; i <= sz; i++) { for(int j = i + 1; j <= sz; j++) { if(nxt[i][a[j]] == -1) nxt[i][a[j]] = j; } } for(int i = 1; i <= cnt + 1; i++) { for(int j = 1; j <= cnt + 1; j++) { for(int k = 1; k <= cnt + 1; k++) { t[0] = i; t[1] = j; t[2] = k; S = mx = 0; dfs(0, 0, 0); LL tmp = 1; for(int s = 0; s < 3; s++) { if((S & (1 << s))==0&&t[s] == cnt + 1) { tmp *= (LL)(m - cnt); } } f[mx] += tmp; } } } printf("%lld %lld %lld %lld\n", f[0], f[1], f[2], f[3]); } return 0;}
- xtu-1265 Longest Common Subsequence(字符串/计数)
- longest common subsequence
- Longest Common Subsequence
- 10405 Longest Common Subsequence
- UVa10405 - Longest Common Subsequence
- 10405 - Longest Common Subsequence
- UVa10405 - Longest Common Subsequence
- UVaOJ10405 - Longest Common Subsequence
- 10405 - Longest Common Subsequence
- uva10405 Longest Common Subsequence
- 10405 - Longest Common Subsequence
- 10405 - Longest Common Subsequence
- Uva10405 Longest Common Subsequence
- UVaOJ_10405 - Longest Common Subsequence
- Longest common subsequence / substring
- uva10405 - Longest Common Subsequence
- longest common subsequence 问题
- [DP]Longest Common Subsequence
- S5PV210系列(裸机七)之 SDRAM
- 龙芯+Linux:国产CPU和操作系统现状是这样
- DOM对象转化成jQuery对象
- 未能加载文件或程序集“Office, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
- 小白题解 Codeforces 1A Theatre Square
- xtu-1265 Longest Common Subsequence(字符串/计数)
- B
- Java之IO流
- 操作系统银行家算法
- oid android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference的解决
- Java:实现简单的阻塞队列
- 函数返回指针数组
- 系统集成项目管理工程师学习笔记(三)项目管理的一般知识
- 0517