/**
392 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
你可以认为 s 和 t 中仅包含英文小写字母。字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=100)。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
示例 1:
s = "abc", t = "ahbgdc"
返回 true.
示例 2:
s = "axc", t = "ahbgdc"
返回 false.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/is-subsequence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
#include <string.h>
#include <malloc.h>
int isSubsequence(char *s, char *t)
{
int sLen = strlen(s) + 1;
int tLen = strlen(t) + 1;
int *dp = malloc(sLen * tLen * sizeof(int));
memset(dp, 0, sLen * tLen * sizeof(int));
for (int j = 0; j < tLen; j++) {
dp[j] = 1;
}
for (int i = 1; i < sLen; i++) {
for (int j = i; j < tLen; j++) {
dp[i * tLen + j] = (s[i - 1] == t[j - 1]) ? dp[(i - 1) * tLen + j - 1] : dp[i * tLen + j - 1];
}
}
int result = 0;
if (dp[(sLen - 1) * tLen + tLen - 1] == 1) {
result = 1;
}
free(dp);
return result;
}
int main()
{
char *s = "abc";
char *t = "ahdfebiulxciu";
printf("\n%d\n", isSubsequence(s, t));
}