http://codeforces.com/gym/100989/my

B. LCS (B)
time limit per test

0.25 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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
input
3 4
0 0 0 0 0
0 0 1 1 1
0 0 1 1 2
0 1 1 1 2
output
abc
cadb
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime> #define PB push_back
#define MP make_pair
#define FOR1(n) for(int i=0;i<(n);++i)
#define FOR2(l,h) for(int i=(l);i<=(h);++i)
#define FOR3(h,l) for(int i=(h);i>=(l);--i) using namespace std;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII; #define PI acos((double)-1)
#define E exp(double(1))
#define K 1000000+9
char a[100],b[100];
int dp[100][100],n,m;
bool us[100];
int main(void)
{
cin>>n>>m;
for(int i=1; i<=n; i++)
a[i]='a'+i-1;
for(int i=0; i<=n; i++)
for(int j=0; j<=m; j++)
scanf("%d",&dp[i][j]);
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
{
if( dp[i][j]==dp[i-1][j-1]+1&&dp[i][j]!=max(dp[i-1][j],dp[i][j-1]))
{
if(!us[j])
{b[j]=a[i];us[j]=1;}
else
{
char t=a[i];
for(int k=1; k<=n; k++)if(a[k]==t)a[k]=b[j];
for(int k=1; k<=m; k++)if(b[k]==t)b[k]=b[j];
}
}
}
for(int i=1; i<=m; i++)
if(!us[i])b[i]='z';
a[n+1]='\0',b[m+1]='\0';
cout<<a+1<<endl<<b+1<<endl;
return 0;
}

  

cf100989b的更多相关文章

随机推荐

  1. 宝宝书 & 网站

    1. 网站 妈妈帮 宝宝树 2. 图书推荐 育儿百科 育儿经 中国儿童智力方程 聪明宝宝营养食谱1001例

  2. EasyUI DataGrid 编辑单元格

    如下图: 现改为单击某个单元格只对此单元格进行可编辑 <TABLE>标记添加 onClickCell <table id="dg" class="eas ...

  3. 第二百三十五节,Bootstrap栅格系统

    Bootstrap栅格系统 学习要点: 1.移动设备优先 2.布局容器 3.栅格系统 本节课我们主要学习一下 Bootstrap 的栅格系统,提供了一套响应式.移动设备优先的流 式栅格系统. 一.移动 ...

  4. js常用API汇总(转)

    typeof(); 检测数据类型 String(); 转换成字符串 parseInt(); 解析出一个string或number的整数部分 parseFloat(); 解析出一个string的浮点数部 ...

  5. CodeFirst中DbContext动态添加DbSet

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. poj2046

    Gap Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1829   Accepted: 829 Description Le ...

  7. delphi ----Raize(第三方控件) TRzNumericEdit

    一.Raize Edits 1.TRzNumericEdit IntegerOnly属性设置为false,可以输入小数. DisplayFormat := ',0.00;(,0.00)';;//小数默 ...

  8. Oracle Database Documentation

    Oracle数据库的发展简史 ORDBMS对象-关系数据库管理系统 Oracle Schema Objects Oracle Schema Objects——Tables——Overview of T ...

  9. 4.php奇葩的地方,反引号``

    今天我发现我从来没打过这外符号 ` 就是键盘的左上方, 1的左边不需要组合键, 直接按下即可.... 刚开始我还一直在找没找到.....百度一下.才知道

  10. Apache Lucene评分机制的内部工作原理

    Apache Lucene评分机制的内部工作原理' 第5章