Zipper OpenJ_Bailian - 2192 (DP最长公共子串)
作者:互联网
Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.
For example, consider forming "tcraete" from "cat" and "tree":
String A: cat
String B: tree
String C: tcraete
As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming "catrtee" from "cat" and "tree":
String A: cat
String B: tree
String C: catrtee
Finally, notice that it is impossible to form "cttaree" from "cat" and "tree".
Input
The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.
For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.
Output
For each data set, print:
Data set n: yes
if the third string can be formed from the first two, or
Data set n: no
if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.
Sample Input
3 cat tree tcraete cat tree catrtee cat tree cttaree
Sample Output
Data set 1: yes Data set 2: yes Data set 3: no
解题思路:
最长公共子串问题
判断S1和S3的最长公共子串长度加上S2和S3的最长公共子串长度是否等于S3的长度,如果相等,就说明匹配
注意一种特殊情况: cat tree catrere
这里最长公共子串共用了一个t,实际上是不允许的,所以还要判断S1,S2中字符出现的个数和S3出现的字符个数是否相同
#include <iostream>
#include <algorithm>
#include <string>
#include <string.h>
#include <set>
using namespace std;
const int MAXN = 510; //设最大长度为510
int N;
string S1, S2, S3;
int sdp1[MAXN][MAXN]; //S1和S3的公共子串
int sdp2[MAXN][MAXN]; //S2和S3的公共子串
int inputAlpSum[MAXN]; //输入比较
int cmpAlpSum[MAXN]; //输出比较
int tindx = 0;
int main() {
cin >> N;
while (N--) {
cin >> S1 >> S2 >> S3;
memset(sdp1, 0, sizeof(sdp1));
memset(sdp2, 0, sizeof(sdp2));
memset(inputAlpSum, 0, sizeof(inputAlpSum));
memset(cmpAlpSum, 0, sizeof(cmpAlpSum));
set<char> apprAlp; //出现的字符
for (int i = 0; i < S1.length(); ++i) {
inputAlpSum[(int)S1[i]]++;
apprAlp.insert(S1[i]);
}
for (int i = 0; i < S2.length(); ++i) {
inputAlpSum[(int)S2[i]]++;
apprAlp.insert(S2[i]);
}
for (int i = 0; i < S3.length(); ++i) {
cmpAlpSum[(int)S3[i]]++; //相加
apprAlp.insert(S3[i]);
}
//S1和S3比较
for (int i = 1; i <= S1.length(); ++i) {
for (int j = 1; j <= S3.length(); ++j) {
if (S1[i - 1] == S3[j - 1]) {
sdp1[i][j] = sdp1[i - 1][j - 1] + 1;
}
else {
sdp1[i][j] = max(sdp1[i - 1][j], sdp1[i][j - 1]); //输出结果
}
}
}
//S2和S3比较
for (int i = 1; i <= S2.length(); ++i) {
for (int j = 1; j <= S3.length(); ++j) {
if (S2[i - 1] == S3[j - 1]) {
sdp2[i][j] = sdp2[i - 1][j - 1] + 1;
}
else {
sdp2[i][j] = max(sdp2[i - 1][j], sdp2[i][j - 1]); //输出结果
}
}
}
bool flag = true;
if (sdp1[S1.length()][S3.length()] == S1.length() && sdp2[S2.length()][S3.length()] == S2.length()) {
flag = true;
}
else {
flag = false;
}
for (auto x : apprAlp) {
if (inputAlpSum[x] != cmpAlpSum[x]) {
flag = false; //否定
break;
}
}
printf("Data set %d: ", ++tindx);
if (flag) {
printf("yes\n");
}
else {
printf("no\n");
}
}
system("PAUSE");
return 0;
}
标签:set,OpenJ,int,S1,tree,Bailian,S3,2192,cat 来源: https://blog.csdn.net/alex1997222/article/details/88984530