All in All
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 26071 | Accepted: 10570 |
Description
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.
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
The input contains several testcases. Each is specified by two strings s, t of alphanumeric ASCII characters separated by whitespace.The length of s and t will no more than 100000.
Output
For each test case output "Yes", if s is a subsequence of t,otherwise output "No".
Sample Input
sequence subsequence person compression VERDI vivaVittorioEmanueleReDiItalia caseDoesMatter CaseDoesMatter
Sample Output
Yes No Yes No
Source
水题。其实就是poj3267的一段子程序拿来又出了一道。
基本思想用贪心。
两个指针,i指向模式串,j指向母串。j一直往后扫,一旦发现与i指向的字符相同,i++。
这样当i走完模式串,也就说明模式串被匹配到了。
这里大家可能会困惑,如果有多个匹配,这样求得的是哪个匹配呢?
答案应该是,所有匹配到的字符 都尽可能排在前面的 那种情况。换句话说,只要匹配到了,i就可以++,无需考虑后面的。是满足贪心性质的。
这个与连续字串匹配是不同的:
例如:
在abzdabc中,如果需要匹配子序列abc,则会匹配到abzdabc. 只需要直接扫即可。并不需要kmp之类的优化。
而如果在abzdabc中,匹配子串abc,则只能匹配到abzdabc. 不具有贪心性质。暴力只能用o(n*m)复杂度,当然用kmp可以优化到O(n+m)。
这段子程序很经典,感受一下:
for (i = 0, j = 0; i < lens, j < lent; j++) {
if (s[i] == t[j]) i++;
}
if (i == lens) cout << "Yes" << endl;
else cout << "No" << endl;
提交记录:
1.RE 。。。居然是因为数组开小了。人家是100000, 我开了10000
2.Accepted!
#include
#include
#include
#include
#include
#include
#include
#include
#include