Subsequence
总提交:19 测试通过:13
描述
We say that a string A is a subsequence of another string B,if there is a number sequence C which satisfied the following conditions:
a.C has exactly length(A) numbers;
b.0<C[0]<c[1]<…<c[Lenth(A)-1]<Lenth(B);
c.A[i]=B[C[i]] for each number i(i<=i<Lenth(A)).
For example, “abcd” is a subsequense of”aaaaaabbbcd”,while “abcd” is not a subquence of “aaaaacccdb”.Of course any string is a subsequence of itself.
In this problem,you are given a long string A and any short string Bi.You need to write a fast program which tells whether Bi is a consequence of A.
输入
On the first line input,there is a positive integer T<=20 specifying the number of test cases fpllow.
The first line eace tst case give the string A.The second line contains an integer M(0<M<=10000),which is the number of short string Bi.And the next following M lines each contain a string Bi.
The length of A will be no more than100000 and the length of all Bi will be no more than 50.All the string will have at least one letter,and all the letters will appear in lowercase
输出
The putout of eace test case contains M+1 lines.The first line should be “Case X:”( quotes for clariy) where X is the number of the test case(starting at 1).
The M lines follow:for each Bi,if it’s a subsequence of A outputb”Yes”,otherwise output “No”.
样例输入
subsequence
2
sequence
bus
problem
1
rom
样例输出
Yes
No
Case 2:
Yes
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<string>
using std::string;
bool check(const string& src, const string& str){
string::size_type src_len(src.length()), str_len(str.length());
for (string::size_type src_counter(0), str_counter(0); str_counter != str_len; ++str_counter){
if (src[src_counter] == str[str_counter]){
++src_counter;
}
if (src_counter == src_len){
return true;
}
}
return false;
}
int main(void){
string src, str;
unsigned short int times, sub_times;
cin >> times;
for (unsigned short int counter(0); counter != times; ++counter){
cin >> str;
cin >> sub_times;
cout << "Case " << counter + 1 << ":" << endl;
while (sub_times--){
cin >> src;
cout << (check(src, str) ? "Yes" : "No") << endl;
}
}
return 0;
}