[csu/coj 1078]多个序列的最长公共子序列

题意:给n个序列,同一个序列里面元素互不相同,求它们的最长公共子序列。

思路:任取一个序列,对于这个序列里面的两个数ai,aj(i<j),如果对于其它每一个序列,都出现过ai,aj,且ai在aj之前出现,那么i到j连一条长度为1的有向边,完美转化为DAG最长路。需要注意:对于某个数,如果某个序列没出现那么这个点的答案应该为-INF,表示这个点表示的状态不合法。

代码:

  1 #pragma comment(linker, "/STACK:10240000,10240000")

  2 

  3 #include <iostream>

  4 #include <cstdio>

  5 #include <algorithm>

  6 #include <cstdlib>

  7 #include <cstring>

  8 #include <map>

  9 #include <queue>

 10 #include <deque>

 11 #include <cmath>

 12 #include <vector>

 13 #include <ctime>

 14 #include <cctype>

 15 #include <stack>

 16 #include <set>

 17 #include <bitset>

 18 #include <functional>

 19 #include <numeric>

 20 #include <stdexcept>

 21 #include <utility>

 22 

 23 using namespace std;

 24 

 25 #define mem0(a) memset(a, 0, sizeof(a))

 26 #define mem_1(a) memset(a, -1, sizeof(a))

 27 #define lson l, m, rt << 1

 28 #define rson m + 1, r, rt << 1 | 1

 29 #define define_m int m = (l + r) >> 1

 30 #define rep_up0(a, b) for (int a = 0; a < (b); a++)

 31 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)

 32 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)

 33 #define rep_down1(a, b) for (int a = b; a > 0; a--)

 34 #define all(a) (a).begin(), (a).end()

 35 #define lowbit(x) ((x) & (-(x)))

 36 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}

 37 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}

 38 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}

 39 #define pchr(a) putchar(a)

 40 #define pstr(a) printf("%s", a)

 41 #define sstr(a) scanf("%s", a)

 42 #define sint(a) scanf("%d", &a)

 43 #define sint2(a, b) scanf("%d%d", &a, &b)

 44 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)

 45 #define pint(a) printf("%d\n", a)

 46 #define test_print1(a) cout << "var1 = " << a << endl

 47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl

 48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl

 49 #define mp(a, b) make_pair(a, b)

 50 #define pb(a) push_back(a)

 51 

 52 typedef long long LL;

 53 typedef pair<int, int> pii;

 54 typedef vector<int> vi;

 55 

 56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};

 57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };

 58 const int maxn = 1e3 + 7;

 59 const int md = 1e9 + 7;

 60 const int inf = 1e9 + 7;

 61 const LL inf_L = 1e18 + 7;

 62 const double pi = acos(-1.0);

 63 const double eps = 1e-6;

 64 

 65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}

 66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}

 67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}

 68 template<class T>T condition(bool f, T a, T b){return f?a:b;}

 69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}

 70 int make_id(int x, int y, int n) { return x * n + y; }

 71 

 72 struct Graph {

 73     vector<vector<int> > G;

 74     void clear() { G.clear(); }

 75     void resize(int n) { G.resize(n + 2); }

 76     void add(int u, int v) { G[u].push_back(v); }

 77     vector<int> & operator [] (int u) { return G[u]; }

 78 };

 79 Graph G;

 80 

 81 int mp[12][12 * maxn];

 82 int dp[maxn], a[12][maxn], b[12 * maxn];

 83 bool vis[maxn];

 84 

 85 int dfs(int pos) {

 86     if (vis[pos]) return dp[pos];

 87     vis[pos] = true;

 88     int sz = G[pos].size();

 89     rep_up0(i, sz) {

 90         max_update(dp[pos], dfs(G[pos][i]) + 1);

 91     }

 92     return dp[pos];

 93 }

 94 

 95 int main() {

 96     //freopen("in.txt", "r", stdin);

 97     int n, m;

 98     while (cin >> n >> m) {

 99         rep_up0(i, n) {

100             rep_up0(j, m) {

101                 sint(a[i][j]);

102                 b[i * m + j] = a[i][j];

103             }

104         }

105         sort(b, b + n * m);

106         int sz = unique(b, b + n * m) - b;

107         mem0(mp);

108         rep_up0(i, n) {

109             rep_up0(j, m) {

110                 a[i][j] = lower_bound(b, b + sz, a[i][j]) - b;

111                 mp[i][a[i][j]] = j + 1;

112             }

113         }

114         G.clear();

115         G.resize(m);

116         rep_up0(i, m) dp[i] = 1;

117         rep_up0(i, m) {

118             rep_up1(j, n - 1) {

119                 if (!mp[j][a[0][i]]) {

120                     dp[i] = -inf;

121                     break;

122                 }

123             }

124         }

125         rep_up0(i, m) {

126             for (int j = i + 1; j < m; j ++) {

127                 bool ok = true;

128                 int x = a[0][i], y = a[0][j];

129                 rep_up1(k, n - 1) {

130                     if (!(mp[k][x] && mp[k][y] && mp[k][x] < mp[k][y])) {

131                         ok = false;

132                         break;

133                     }

134                 }

135                 if (ok) G[i].push_back(j);

136             }

137         }

138         int ans = 0;

139         mem0(vis);

140         rep_up0(i, m) max_update(ans, dfs(i));

141         cout << ans << endl;

142     }

143     return 0;

144 }
View Code

 

你可能感兴趣的:(序列)