日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Gargari and Permutations CodeForces - 463D(建图+记忆化搜索)

發(fā)布時間:2023/12/15 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Gargari and Permutations CodeForces - 463D(建图+记忆化搜索) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Gargari got bored to play with the bishops and now, after solving the problem about them, he is trying to do math homework. In a math book he have found k permutations. Each of them consists of numbers 1,?2,?…,?n in some order. Now he should find the length of the longest common subsequence of these permutations. Can you help Gargari?

You can read about longest common subsequence there: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

Input
The first line contains two integers n and k (1?≤?n?≤?1000; 2?≤?k?≤?5). Each of the next k lines contains integers 1,?2,?…,?n in some order — description of the current permutation.

Output
Print the length of the longest common subsequence.

Examples
Input
4 3
1 4 2 3
4 1 2 3
1 2 4 3
Output
3
Note
The answer for the first test sample is subsequence [1, 2, 3].
思路:找出k個序列中都存在的最長的序列。那么這個串中的元素在這k個序列的相對位置都是相同的,那么我們找出在所有的數(shù)對(x,y),這種數(shù)對的特性是在k個序列中x都在y的前面,這樣的序列,我們給他們建一條邊。然后記憶化搜索跑圖去得到一個最大值就可以了。
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;const int maxx=1e6+100; const int maxm=1e3+100; struct edge{int to,next; }e[maxx<<1]; int a[maxx],head[maxx<<1],dp[maxm]; int n,m,tot;inline void init() {tot=0;memset(head,-1,sizeof(head));memset(dp,-1,sizeof(dp)); } inline void add(int u,int v) {e[tot].to=v,e[tot].next=head[u],head[u]=tot++; } inline int dfs(int u) {if(dp[u]!=-1) return dp[u];dp[u]=1;int sum=0;for(int i=head[u];i!=-1;i=e[i].next){int to=e[i].to;sum=max(sum,dfs(to));}dp[u]+=sum;return dp[u]; } int main() {scanf("%d%d",&n,&m);map<pair<int,int>,int> mp;init();for(int i=1;i<=m;i++){for(int j=1;j<=n;j++) scanf("%d",&a[j]);for(int x=1;x<=n;x++)for(int y=x+1;y<=n;y++) mp[make_pair(a[x],a[y])]++;}for(int i=1;i<=n;i++)for(int j=1;j<=n;j++) if(mp[make_pair(i,j)]==m) add(i,j);int _max=0;for(int i=1;i<=n;i++) _max=max(_max,dfs(i));cout<<_max<<endl;return 0; }

努力加油a啊,(o)/~

總結(jié)

以上是生活随笔為你收集整理的Gargari and Permutations CodeForces - 463D(建图+记忆化搜索)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。