题目大意:
输入两个字符串 s 和 t 判断 是否可以从 t 中删除0个或多个字符(其它字符顺序不变),得到字符串 s 。例如, abcde可以得到 bce 而不能得到 dc。
Input
sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter
Output
Yes
No
Yes
No
下面给出C与C++的实现(区别不大)
1C语言实现:
#include<stdio.h>
#include<string.h>
#define maxn 1000000
int main()
{
int i, j;
char s[maxn], t[maxn];
while(scanf("%s",s) == 1){
scanf("%s",t);
int lens = strlen(s);
int lent = strlen(t);
i = 0;
j = 0;
int cnt = 0;
while(i < lens && j < lent){
if(s[i] != t[j])
j++;
else if(s[i] == t[j]){
i++;
j++;
cnt++;
}
if(cnt+lent-j < lens)
break;
}
if(cnt == lens)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}
2 C++实现(string 类):
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
int main()
{
int i, j;
string s, t;
while(cin >> s >> t){
i = 0;
j = 0;
int lens = s.length();
int lent = t.length();
int cnt = 0;
while(i < lens && j < lent){
if(s[i] != t[j])
j++;
else if(s[i] == t[j]){
i++;
j++;
cnt++;
}
if(cnt + lent - j < lens)
break;
}
if(cnt == lens)
cout << "Yes" <<endl;
else
cout << "No" << endl;
}
return 0;
}
两种实现没有本质区别, 需要注意的是 C语言的需要定义字符数组 实现字符串, 这里就涉及到了假定的字符数组大小问题,刚开始就RTE(运行时错误) 就是因为 我申请的
数组小了 导致数据溢出。但是 C++语言的string类就可以避免这个问题,因为它可以根据输入数据大小自动调整的。