cf100989b
http://codeforces.com/gym/100989/my
0.25 seconds
256 megabytes
standard input
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?
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.
Print string A on the first line and string B on the second line. Both strings should contain only lowercase English letters.
3 4
0 0 0 0 0
0 0 1 1 1
0 0 1 1 2
0 1 1 1 2
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的更多相关文章
随机推荐
- ThreadLocal分析
我们再介绍一个在多线程环境中经常使用的类ThreadLocal,它是java为解决多线程程序的并发问题提供了一种新的方向,使用这个ThreadLocal类可以帮助开发者很简单地编写出简洁的程序,并且是 ...
- jq 选择器基础及拓展
jquery 用的很多,所以jq的选择器就很受欢迎,但是用的过程中有一些小问题,如果不点透就永远不知道. 1:ID选择器:$("#ID"); 得到一个指定对应,并且只能得到一个对象 ...
- IOS 预览pdf,word文档的集中方式
在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...
- iOS UITableView划动删除的实现
标签:划动删除 iphone 滑动删除 ios UITableView 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://rainb ...
- 决策树ID3算法python实现 -- 《机器学习实战》
from math import log import numpy as np import matplotlib.pyplot as plt import operator #计算给定数据集的香农熵 ...
- Audit logon events&Logon type
表一.Logon type 表二.Audit logon events 表三.Logon type details Logon type Logon title Description 2 Inter ...
- C# 利用StringBuilder提升字符串拼接性能
一个项目中有数据图表呈现,数据量稍大时显得很慢. 用Stopwatch分段监控了一下,发现耗时最多的函数是SaveToExcel 此函数中遍列所有数据行,通过Replace替换标签生成Excel行,然 ...
- linux下EOF写法梳理 自动新建分区并挂载的脚本
linux下EOF写法梳理 - 散尽浮华 - 博客园 https://www.cnblogs.com/kevingrace/p/6257490.html 在平时的运维工作中,我们经常会碰到这样一个场景 ...
- CSRF Cross-site request forgery
w 跨站请求伪造目标站---无知用户---恶意站 http://fallensnow-jack.blogspot.com/2011/08/webgoat-csrf.html https://wiki. ...
- Java基础 - 面向对象 - 构造方法
在类中除了成员方法之外,还存在一种特殊类型的方法,那就是构造方法.构造方法是一个与类同名的方法,对象的创建就是通过构造方法完成的.每当类实例化一个对象时,类都会自动调用构造方法. 构造方法的特点: 构 ...