本文主要是介绍习题 3 -9 子序列(All in All) UVa 10304,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目大意:
输入两个字符串 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");elseprintf("No\n");} return 0;}
#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类就可以避免这个问题,因为它可以根据输入数据大小自动调整的。
这篇关于习题 3 -9 子序列(All in All) UVa 10304的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!