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. 移动端上下滑动事件之--坑爹的touch.js

    原文   http://blog.csdn.net/minidrupal/article/details/39611605 移动端页面的盛行,微信的便利的页面推广等等,让越来越多的css3效果和htm ...

  2. 【BZOJ】2101: [Usaco2010 Dec]Treasure Chest 藏宝箱(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2101 这个dp真是神思想orz 设状态f[i, j]表示i-j先手所拿最大值,注意,是先手 所以转移 ...

  3. cinder服务端的keystone认证机制

    keystone在openstack中的地位 Keystone作为OpenStack中的身份管理与授权模块,主要实现系统用户的身份认证.基于角色的授权管理.其他OpenStack服务的地址发现和安全策 ...

  4. 【Python】Webpy

    http://webpy.org/install.zh-cn 官网学习,对于No socket could be created 一般是默认的8080端口已经被某些服务占用,可以换一个端口.

  5. gcc/g++ 实战之编译的四个过程

    gcc和g++分别是GNU(一个开源组织)的c&c++编译器   对于.c后缀的文件,gcc把它当做是C程序,g++当做是C++程序:对于.cpp后缀的文件,gcc和g++都会当做c++程序. ...

  6. 尼康D90多点对焦

    11点对焦 上市时间 2008 类型 单反数码相机 对焦方式 单区域AF:在选择区域内只对焦于目标可以从11个AF点传感器中的任意一个中选择 动态区域AF:对焦于选择区域的目标上,如果目标离开原来位置 ...

  7. 面试题思考:Cookie 和 Session的区别

    面试回答: 1.cookie数据存放在客户的浏览器上,session数据放在服务器上. 2.cookie不是很安全,别人可以分析存放在本地的cookie并进行cookie欺骗,考虑到安全应当使用ses ...

  8. Android中流式布局和热门标签

    1.流式布局特点.应用场景.2.自定义ViewGroup (1)onMeasure:测量子View的宽和高,设置自己的宽和高. (2)onLayout:设置子View的位置. onMeasure:根据 ...

  9. [Go语言]从Docker源码学习Go——function和method

    function和method关系 method是针对某一类型定义的function, function可以单独调用,method必须针对某一类型的实例进行调用 //function 调用方式 pac ...

  10. 《从零开始学Swift》学习笔记(Day 63)——Cocoa Touch设计模式及应用之单例模式

    原创文章,欢迎转载.转载请注明:关东升的博客 什么是设计模式.设计模式是在特定场景下对特定问题的解决方案,这些解决方案是经过反复论证和测试总结出来的.实际上,除了软件设计,设计模式也被广泛应用于其他领 ...