本文主要是介绍codeforces 463D Gargari and Permutations,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
从所给的序列中找出它们的最长公共子序列。
思路:
DAG(有向无环图)+BFS。
如果数值 i 在所有序列中都在 j 前面。则i -> j连一条有向边。(好像还有个dp的思路的)
参考:http://www.cnblogs.com/hujunzheng/p/3947392.html
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <queue>
typedef long long ll;
using namespace std;
const int MAXN = 1005;
int n, k;
int rec[MAXN][MAXN], rudu[MAXN];
int row[MAXN];
vector <int> gra[MAXN];
bool inq[MAXN];
int dis[MAXN];
inline int read()
{char t = getchar();int res = 0;while(t >='0' && t <= '9') {res = res*10 + (t-'0'); t = getchar();}return res;
}
int solve()
{queue <int> q;for(int i = 1;i <= n; i++)if(rudu[i] == 0){q.push(i);inq[i] = true;dis[i] = 1;}while(!q.empty()){int u = q.front();q.pop();inq[u] = false;for(int i = 0;i < gra[u].size(); i++){int v = gra[u][i];if(dis[v] < dis[u] + 1){dis[v] = dis[u]+1;if(!inq[v])q.push(v);}}}int res = 0;for(int i = 1;i <= n; i++)res = max(res, dis[i]);return res;
}int main()
{n = read(), k = read();for(int i = 1;i <= k; i++){for(int j = 1;j <= n; j++)row[j] = read();for(int j = 1;j < n; j++)for(int z = j+1;z <= n; z++)rec[row[j]][row[z]] ++;}//for(int i = 1;i <= n; i++)for(int j = 1;j <= n; j++)if(rec[i][j] == k){gra[i].push_back(j);rudu[j] ++;}int res = solve();cout<<res<<endl;return 0;
}
这篇关于codeforces 463D Gargari and Permutations的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!