题目大意:给出字符串s和t,问s是否是t的子串。s若去掉某些字符能和t一样,那么t是s的子串。
解题思路:匹配字符。t的每一个字符和s中的字符匹配。注意这里的字符数组大小要开大点。
代码:
#include <stdio.h>
#include <string.h> const int N = 1000005;
char s[N], t[N]; bool match () { int i = 0;
int lens = strlen(s);
int lent = strlen(t);
for (int j = 0; j < lent; j++) { if (i == lens)
return true;
if (lens - i > lent - j)
return false;
if (s[i] == t[j])
i++;
}
if (i == lens)
return true;
return false;
} int main () { while (scanf ("%s", s) != EOF) { scanf ("%s", t);
printf ("%s\n", match()? "Yes" :"No");
}
return 0;
}