UVa 10340 子序列

来源:互联网 发布:网络教育专科学费多少 编辑:程序博客网 时间:2024/05/20 16:11

10340 - All in All

Time limit: 3.000 seconds

Problem E

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

遍历数组t寻找s各元素的对应元素,过程中若有一次找不到,返回No
用c++的string类更简单
#include <stdio.h>#include <string.h>char s[100000];char t[100000];int yes_or_no(const char *s,const char *t);int main(int argc, const char * argv[]) {    while (scanf("%s %s",s,t)!= EOF) {        if (yes_or_no(s, t)) {            printf("Yes\n");        }        else            printf("No\n");    }    return 0;}int yes_or_no(const char *s,const char *t){    int j = 0;    for (int i = 0; i < strlen(s); ++i,++j) {        while (j < strlen(t) && t[j]!=s[i])            ++j;        if (j == strlen(t))            return 0;    }    return 1;}


0 0