这道题也不能说是dp,感觉dp没那么强。
题目是说,给定m(m<=10)个数列,每个数列包含n个数(n<=1e5).问这m个数列中有多少个相同的子数列(连续的哦)
重点在于透过第一个数列,即以第一个数列为样本,找到符合条件的子数列。从第一个数列的最后开始向前遍历,每次判断m条数列是不是都满足第i个位子的数字x的后面是数字y。如果不是,这个位置dp值记为1,否则就是dp【i+1】+1。答案加上每个位子的dp值即可。
//#pragma GCC optimize(3) //#pragma comment(linker, "/STACK:102400000,102400000") //c++ // #pragma GCC diagnostic error "-std=c++11" // #pragma comment(linker, "/stack:200000000") // #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << "\n"; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0) #define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行 #define REP(i , j , k) for(int i = j ; i < k ; ++i) #define max3(a,b,c) max(max(a,b), c); //priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x7f7f7f7f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e8+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } /*-----------------------showtime----------------------*/ const int maxn = 1e5+9; ll mp[20][maxn],a[20][maxn],dp[maxn]; int main(){ int n,m; scanf("%d%d", &n, &m); for(int i=1; i<=m; i++){ for(int j=1; j<=n; j++){ scanf("%lld", &mp[i][j]); a[i][mp[i][j]] = j; } } ll ans = 0; for(int i=n; i>=1; i--){ int flag = 1, val = mp[1][i]; dp[a[1][val]] = 1; for(int j=2; j<=m; j++){ if(a[j][val]+1>n || a[1][val]+1>n || mp[j][a[j][val]+1] != mp[1][a[1][val]+1]){ flag = 0; } } if(flag)dp[i] = dp[i+1] + 1; ans += dp[i]; } printf("%lld\n", ans); return 0; }