C3-UVa10340-All in All

Posted on 2020-06-28 10:57  lemonforce  阅读(112)  评论(0编辑  收藏  举报

平台:

UVa Online Judge

題號:

10340 - All in All

題目連結:

q

題目說明:

输入两个字符串s和t,判断是否可以从t中删除0个或多个字符(其他字符顺序不变),得到字符串s。例如,abcde可以得到bce,但无法得到dc。

範例輸入:

sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter

範例輸出:

Yes
No
Yes
No

解題方法:

这题巨简单。只需要逐个比较2个字符串,一个都加1,一个只有相等才加1。最后看看只有相等才加1的字符串是不是跑完了即可。

程式碼:

 1 #include <iostream>
 2 #include <string>
 3 
 4 int main() {
 5     using namespace std;
 6     string s, t;
 7     while (cin >> s >> t) {
 8         int i = 0, j = 0;
 9         bool flag = false;
10         while (t[j] != '\0') {
11             if (s[i] ==  t[j]) {
12                 i++;
13             }
14             if (i == s.size()) {
15                 flag = true;
16                 break;
17             }
18             j++;
19         }
20         if (flag) {
21             cout << "Yes\n";
22         }
23         else {
24             cout << "No\n";
25         }
26     }
27 }