UVa10340.All in All

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1281

13845990 10340 All in All Accepted C++ 0.026 2014-07-07 15:05:55

All in All

Input: standard input

Output: standard output

Time Limit: 2 seconds

Memory Limit: 32 MB

You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way. Because of pending patent issues we will not discuss in detail how the strings are generated and inserted into the original message. To validate your method, however, it is necessary to write a program that checks if the message is really encoded in the final string.

Given two strings s and t, you have to decide whether s is a subsequence of t, i.e. if you can remove characters from t such that the concatenation of the remaining characters is s.

Input Specification

The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace. Input is terminated by EOF.

Output Specification

For each test case output, if s is a subsequence of t.

Sample Input

sequence subsequence
person compression
VERDI vivaVittorioEmanueleReDiItalia
caseDoesMatter CaseDoesMatter

Sample Output

Yes
No
Yes

 No


 解题思路:小心特判就好。以前在POJ上写过此题,其中有一组数据是匹配串和模式串相同的情况,要输出Yes。有写简洁的方法并不需要特判。从中也找到了自己的不足,应该多写习题,多多练习,这样才能简化代码。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cstdlib>
 5 #include <cctype>
 6 #include <cmath>
 7 #include <string>
 8 #include <algorithm>
 9 #include <numeric>
10 using namespace std;
11 
12 int main() {
13     string pattern, matcher;
14     while (cin >> pattern >> matcher) {
15         if(pattern == matcher) {
16             cout << "Yes" << endl;
17             continue;
18         }
19 
20         if(pattern.size() >= matcher.size()) {
21             cout << "No" << endl;
22             continue;
23         }
24         int cnt = 0; //匹配长度
25 
26         for(int i = 0, j = 0; j < matcher.size(); j ++) {
27             if(pattern[i] == matcher[j]) {
28                 i++; cnt++;
29                 if(cnt == pattern.size()) {
30                     break;
31                 }
32             }
33         }
34 
35         if(cnt == pattern.size()) {
36             cout << "Yes" << endl;
37         } else {
38             cout << "No" << endl;
39         }
40     }
41     return 0;
42 }
原文地址:https://www.cnblogs.com/Destiny-Gem/p/3830603.html