1936 All in All:超喜欢的模板题:字符串t能否认s做父

题目大意

题目链接

给定两个字符串s和t,您必须确定s是否为t的子序列,即是否可以从t中删除字符以使其余字符的串联为s。

输入

输入包含几个测试用例。 每个字符串由两个字符串s和t指定,两个字符串由空格隔开的字母数字ASCII字符.s和t的长度不得超过100000。

思路分析

s是否包含在t中,我们可以将其转化为,s与t的最长公共子串问题,如果最长的公共字串刚好等于s,那么显然就是包含在内了。注意这里的字符串长度还是蛮长的,需要滚动数组优化一下。

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<string.h>
#include<queue>
#include<stdio.h>
#include<string>
using namespace std;

string s1, s2;
int dp[2][100000];
int main() {
	while (cin >> s1 >> s2) {
		memset(dp, 0, sizeof(dp));
		//第一行,第一列都是0
		for (int i = 0; i < s1.size(); i++) {
			for (int j = 0; j < s2.size(); j++) {
				int x = i + 1, y = j + 1;
				if (s1[i] == s2[j]) {
					dp[x % 2][y] = dp[(x - 1) % 2][y - 1] + 1;
				}
				else
					dp[x % 2][y] = max(dp[(x - 1) % 2][y], dp[x % 2][y - 1]);
			}
		}
		if (dp[s1.size() % 2][s2.size()] == s1.size()) cout << "Yes" << endl;//最长公共字串恰好就是s1的长度
		else cout << "No" << endl;
	}
}
发布了186 篇原创文章 · 获赞 13 · 访问量 9289

猜你喜欢

转载自blog.csdn.net/csyifanZhang/article/details/105262371
ALL
今日推荐