Given two strings of lowercase letters, a and b, print the longest string x of lowercase letters such that there is a permutation of x that is a subsequence of a and there is a permutation of x that is a subsequence of b.
Input
Input file contains several lines of input. Consecutive two lines make a set of input. That means in the input file line 1 and 2 is a set of input, line3 and 4 is a set of input and so on. The first line of a pair contains a and the second contains b. Each string is on a separate line and consists of at most 1000 lowercase letters.
Output
For each set of input, output a line containing x. If several x satisfy the criteria above, choose the first one in alphabetical order.
Sample Input:
pretty
women
walking
down
the
street
Sample Output:
e
nw
et
<span style="font-size:14px;">#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max=1000;
char str1[Max+10];
char str2[Max+10];
int maxlen[Max+10][Max+10];
int p[Max+10][Max+10];
void print(int i,int j){
if(i==0||j==0)
return;
else if(p[i][j]==1){
print(i-1,j-1);
printf("%c",str1[i-1]);
}
else if(p[i][j]==2)
print(i-1,j);
else if(p[i][j]==3)
print(i,j-1);
}
int main(){
while(gets(str1)){
gets(str2);
int l1=strlen(str1);
int l2=strlen(str2);
sort(str1,str1+l1);
sort(str2,str2+l2);
for(int i=0;i<=l1;i++)
maxlen[i][0]=0;
for(int i=0;i<=l2;i++)
maxlen[0][i]=0;
for(int i=1;i<=l1;i++){
for(int j=1;j<=l2;j++){
if(str1[i-1]==str2[j-1]){
maxlen[i][j]=maxlen[i-1][j-1]+1;
p[i][j]=1;
}
else if(maxlen[i-1][j]>maxlen[i][j-1]){
maxlen[i][j]=maxlen[i-1][j];
p[i][j]=2;
}
else{
maxlen[i][j]=maxlen[i][j-1];
p[i][j]=3;
}
}
}
print(l1,l2);
printf("\n");
}
return 0;
}
</span>
不用二维数组p记录每一格是由哪一步走来的也可以进行打印输出
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max=1000;
char str1[Max+10];
char str2[Max+10];
int maxlen[Max+10][Max+10];
void print(int i,int j){
if(i==0||j==0)
return;
else if(str1[i-1]==str2[j-1]){
print(i-1,j-1);
printf("%c",str1[i-1]);
}
else if(maxlen[i][j]==maxlen[i-1][j])
print(i-1,j);
else if(maxlen[i][j]==maxlen[i][j-1])
print(i,j-1);
}
int main(){
while(gets(str1)){
gets(str2);
int l1=strlen(str1);
int l2=strlen(str2);
sort(str1,str1+l1);
sort(str2,str2+l2);
for(int i=0;i<=l1;i++)
maxlen[i][0]=0;
for(int i=0;i<=l2;i++)
maxlen[0][i]=0;
for(int i=1;i<=l1;i++){
for(int j=1;j<=l2;j++){
if(str1[i-1]==str2[j-1])
maxlen[i][j]=maxlen[i-1][j-1]+1;
else if(maxlen[i-1][j]>maxlen[i][j-1])
maxlen[i][j]=maxlen[i-1][j];
else maxlen[i][j]=maxlen[i][j-1];
}
}
print(l1,l2);
printf("\n");
}
return 0;
}
非递归输出:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max=1000;
char str1[Max+10];
char str2[Max+10];
int maxlen[Max+10][Max+10];
int p[Max+10][Max+10];
void print(){
int k=0;
int i=strlen(str1);
int j=strlen(str2);
int lcs[Max+10];
while(i>0&&j>0){
if(p[i][j]==1){
lcs[k++]=str1[i-1];
i--;
j--;
}
else if(p[i][j]==2)
i--;
else if(p[i][j]==3)
j--;
}
for(int h=k-1;h>=0;h--)
printf("%c",lcs[h]);
}
int main(){
while(gets(str1)){
gets(str2);
int l1=strlen(str1);
int l2=strlen(str2);
sort(str1,str1+l1);
sort(str2,str2+l2);
for(int i=0;i<=l1;i++)
maxlen[i][0]=0;
for(int i=0;i<=l2;i++)
maxlen[0][i]=0;
for(int i=1;i<=l1;i++){
for(int j=1;j<=l2;j++){
if(str1[i-1]==str2[j-1]){
maxlen[i][j]=maxlen[i-1][j-1]+1;
p[i][j]=1;
}
else if(maxlen[i-1][j]>maxlen[i][j-1]){
maxlen[i][j]=maxlen[i-1][j];
p[i][j]=2;
}
else{
maxlen[i][j]=maxlen[i][j-1];
p[i][j]=3;
}
}
}
print();
printf("\n");
}
return 0;
}
节省记忆空间 只用一个一位数组maxlen[l2] 同时要记录左上方的值
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int Max=1000;
char str1[Max+10];
char str2[Max+10];
int maxlen[Max+10],t[Max+10];
int p[Max+10][Max+10];
void print(int i,int j){
if(i==0||j==0)
return;
else if(p[i][j]==1){
print(i-1,j-1);
printf("%c",str1[i-1]);
}
else if(p[i][j]==2)
print(i-1,j);
else if(p[i][j]==3)
print(i,j-1);
}
int main(){
while(gets(str1)){
gets(str2);
int l1=strlen(str1);
int l2=strlen(str2);
sort(str1,str1+l1);
sort(str2,str2+l2);
for(int i=0;i<=l2;i++){
maxlen[i]=0;
t[i]=0;
}
for(int i=1;i<=l1;i++){
for(int j=1;j<=l2;j++){
if(str1[i-1]==str2[j-1]){
t[j]=maxlen[j];
maxlen[j]=t[j-1]+1;
p[i][j]=1;
}
else if(maxlen[j-1]>maxlen[j]){
t[j]=maxlen[j];
maxlen[j]=maxlen[j-1];
p[i][j]=3;
}
else{
t[j]=maxlen[j];
maxlen[j]=maxlen[j];
p[i][j]=2;
}
}
}
print(l1,l2);
printf("\n");
}
return 0;
}