# poj 2192 Zipper

225人阅读 评论(0)

1.确定每个字符串与合并之后的字符串在字符串的顺序不变的情况下，各个字符串与合并之后的字符串的正确字符的个数
2.在1的前提下，遍历求出当前元素之前，该字符串与合并之后的字符串相同元素的最大数

#include<iostream>
#include<string.h>
#include<math.h>
#include<fstream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<vector>
#define MAXSIZE 100
using namespace std;
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
int n = 0;
char s1[210], s2[210], s3[420];
int dp[210][210];
int main()
{
//freopen("data_2192.txt","r",stdin);
scanf("%d", &n);
for (int k = 1; k <= n; k++)
{
memset(dp, 0, sizeof(dp));
scanf("%s%s%s", s1, s2, s3);
int len1 = strlen(s1);
int len2 = strlen(s2);
for (int i = 1; i <= len1; i++)
{
if (s1[i - 1] == s3[i - 1])
{
dp[i][0] = max (dp[i][0], dp[i - 1][0] + 1);
}else
{
dp[i][0] = dp[i - 1][0];
}
}
for (int i = 1; i <= len2; i++)
{
if (s2[i - 1] == s3[i - 1])
{
dp[0][i] = max (dp[0][i], dp[0][i - 1] + 1);
}else
{
dp[0][i] = dp[0][i - 1];
}
}
for (int i = 1; i <= len1; i++)
{
for (int j = 1; j <= len2; j++)
{
dp[i][j] = max (dp[i - 1][j], dp[i][j - 1]);
if (s1[i - 1] == s3[i + j - 1])
{
dp[i][j] = max (dp[i][j], dp[i - 1][j] + 1);
}
if (s2[j - 1] == s3[i + j - 1])
{
dp[i][j] = max (dp[i][j], dp[i][j - 1] + 1);
}
}
}
if (dp[len1][len2] == len1 + len2)
{
printf("Data set %d: yes\n", k);
}else
{
printf("Data set %d: no\n", k);
}
}

return 0;
}
0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场