标签:div eve 其他 iostream follow cout with only lcs
Although Haneen was able to solve the LCS problem, Dr. Ibrahim is suspicious about whether she really understands the LCS problem or not. He believes that she can‘t write the code on her own, but can only translate the LCS pseudo-code given in class into C++ code without really understanding how it works. Here is the pseudo-code Dr. Ibrahim gave in class:
function LCS (A[1..R], B[1..C])
DP = array(0..R, 0..C)
for i := 0..R
DP[i,0] = 0
for j := 0..C
DP[0,j] = 0
for i := 1..R
for j := 1..C
if A[i] = B[j]
DP[i,j] := DP[i-1,j-1] + 1
else
DP[i,j] := max(DP[i,j-1], DP[i-1,j])
return DP[R,C]
To verify that Haneen understands the LCS problem, Dr. Ibrahim asked her to solve the following problem:
After running the above LCS code on two strings A and B, the 2D array DP is filled with values. Given the 2D array DP, can you guess what A and B are? Any two strings A and B that will produce the given table and contain only lowercase English letters are acceptable.
Can you help Haneen solve this problem?
Input
The first line of input contains two integers R and C (1?≤?R,?C?≤?25), the length of the strings A and B, respectively.
Each of the following R?+?1 lines contains C?+?1 integers, these lines represent the 2D array DP.
It‘s guaranteed that the given table was produced by running the algorithm on two strings that contain only lowercase English letters.
Output
Print string A on the first line and string B on the second line. Both strings should contain only lowercase English letters.
Example
3 4
0 0 0 0 0
0 0 1 1 1
0 0 1 1 2
0 1 1 1 2
abc
cadb
把所有联通的点公用一个字母,注意多对多的字母相等关系。
长度为25,即a-x,所以其他不联通的点都为z
1 #include <iostream> 2 #include <string> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <cstdio> 7 #include <queue> 8 #include <stack> 9 #include <iomanip> 10 #include <vector> 11 #include <set> 12 #include <map> 13 #include <deque> 14 #include <list> 15 #include <bitset> 16 17 using namespace std; 18 19 typedef long long ll; 20 const int MAXN = 1005; 21 const int INF = 0x3f3f3f3f; 22 const int MOD = 1e9 + 7; 23 24 int n, m, dp[55][55]; 25 char a[50], b[50]; 26 int main() 27 { 28 cin >> n >> m; 29 int i, j, k; 30 for(i = 0;i <= n;++i) 31 for(j = 0;j <= m;++j) 32 cin >> dp[i][j]; 33 for(i = 1;i < 30;++i) 34 a[i] = ‘a‘ - 1 + i; 35 for(i = 1;i <= n;++i) 36 for(j = 1;j <= m;++j) 37 { 38 if(dp[i][j] != max(dp[i - 1][j], dp[i][j - 1])) 39 { 40 if(b[j]) 41 { 42 for(k = 1;k <= m;++k) 43 if(b[k] == a[i]) 44 b[k] = b[j]; 45 //把之前与 a[i] 相同的字母都换,所以参考字母 a[i] 不能换 46 for(k = 1;k <= n;++k) 47 if(a[i] == a[k] && k != i) 48 a[k] = b[j]; 49 a[i] = b[j]; 50 } 51 else 52 b[j] = a[i]; 53 } 54 } 55 56 for(i = 1;i <= m;++i) 57 if(!b[i]) 58 b[i] = ‘z‘; 59 60 a[n + 1] = b[m + 1] = 0; 61 cout << (a + 1) << endl << (b + 1) << endl; 62 return 0; 63 }
标签:div eve 其他 iostream follow cout with only lcs
原文地址:https://www.cnblogs.com/shuizhidao/p/9277546.html