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的更多相关文章
随机推荐
- 蓝桥杯 第三届C/C++预赛真题(9) 夺冠概率(手工计算概率)
足球比赛具有一定程度的偶然性,弱队也有战胜强队的可能. 假设有甲.乙.丙.丁四个球队.根据他们过去比赛的成绩,得出每个队与另一个队对阵时取胜的概率表: 甲 乙 丙 丁 甲 - 0.1 0.3 0.5乙 ...
- Domino Web中隐藏附件选择框
只要在Web Form的最后加入以下代码,并选择Pass-Tru HTML,即可隐藏默认的附件选择框. <div id=attach Style="display:none" ...
- 邮件发送模型及其Python应用实例
SMTP(Simple Mail Transfer Protocol) 制定: First:RFC 788 in 1981 Last:RFC 5321 in 2008 端口: TCP 25(SMTP) ...
- 滚动监听: bootstrap 的scrollspy
滚动监听 bootstrap 的scrollspy,需要借助.nav样式,活动的部分是加 .active类.本身导航没有position:fixed,需要自己加入 滚动监听.只有滚动和监听,只有默认锚 ...
- 活动窗口(Active),焦点窗口(Focus)和前景窗口(Foreground)之间的关系
活动窗口(Active),焦点窗口(Focus)和前景窗口(Foreground)之间的关系 任何一个时候,我们的Windows桌面上总有一个最前台的窗口,其实说简单的,就是标题栏变成深蓝色的那个窗口 ...
- python的三元运算
python的三元运算是先输出结果,再判定条件.其格式如下: >>> def f(x,y): return x - y if x>y else abs(x-y) #如果x大于y ...
- 网络编程3 网络编程之缓冲区&subprocess&粘包&粘包解决方案
1.sub简单使用 2.粘包现象(1) 3.粘包现象(2) 4.粘包现象解决方案 5.struct学习 6.粘包现象升级版解决方案 7.打印进度条
- 指定运行Exchange Powershell的Server
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noexit -command ". 'C:\Program Files ...
- DOM 综合练习(二)
// 需求一: 二级联动菜单 <html> <head> <style type="text/css"> select{ width:100px ...
- 《深入理解Linux内核》阅读笔记 --- Chapter 2 Memory Addressing
1.logical address = segment identifier (16bits) + offset (32bits) segment selector其实就是GDT或者LDT的索引,其中 ...