题目大意:给出两个字符串s和t,判断s是不是t的子串(不用连续)
输入:(可以有很多case)
第i个case的s t(长度不超过100000)
输出:Yes/No
分析:用动规的LCS消耗的空间太大了,因为列有100000个,而且只需要判断是否匹配不用开数组,所以直接暴力解决
代码:转载自http://blog.csdn.net/xuezhongfenfei/article/details/8471191
- #include<iostream>
- #include<string>
- using namespace std;
- int main()
- {
- char s[100005],t[100005];
- int i,j,len1,len2;
- while(scanf("%s %s",s,t)!=EOF)
- {
- len1=strlen(s);
- len2=strlen(t);
- i=0;
- j=0;
- while(i<len1 && j<len2)
- {
- if(s[i]==t[j])
- {
- i++;
- j++;
- }
- else
- {
- j++;
- }
- }
- if(i==len1)
- cout<<"Yes"<<endl;
- else
- cout<<"No"<<endl;
- }
- return 0;
- }
下面上化简版:
#include <stdio.h>
char s[100010], t[100010];
int main() {
while(scanf("%s%s", s, t) != EOF) {
int i = 0, j;
for(j = 0; t[j] != 0; ++j)
if(s[i] == t[j]) ++i;
printf("%s\n", s[i] == 0 ? "Yes" : "No");
}
return 0;
}