HDU 5414 CRB and String 【string】

Description

CRB has two strings ss and tt.
In each step, CRB can select arbitrary character cc of ss and insert any character dd (d ≠ cd ≠ c) just after it.
CRB wants to convert ss to tt. But is it possible?

Input

There are multiple test cases. The first line of input contains an integer TT, indicating the number of test cases. For each test case there are two strings ss and tt, one per line.
1 ≤ TT ≤ 105105
1 ≤ |s||s| ≤ |t||t| ≤ 105105
All strings consist only of lowercase English letters.
The size of each input file will be less than 5MB.

Output

For each test case, output “Yes” if CRB can convert s to t, otherwise output “No”.

Sample Input

4
a
b
cat
cats
do
do
apple
aapple

Sample Output

No
Yes
Yes
No

题意:

每组测试数据输入两个字符串a和b,你可以选择字符串a中的任意一个字母x,在这个字母的后面加任意一个不是x的字母。最后使a变成b。如果能变成b输出Yes,否则输出No。(注意yes,no的大小写。。。。)

思路:

仔细分析可以得出yes的条件:
1、如果 a,b从第一个字母开始有若干相同的字母,那么这些相同字母的种类必须相同,个数上,a的相同字母个数要大于等于b相同字母的个数。
2、a必须是b的一个子序列。
想到这两条题目就很简单了~

ac代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
//#include 
using namespace std;
typedef long long ll;
const int maxn = 1e6 + 5;
const int MOD = 1e9 + 7;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
char a[maxn], b[maxn];
int main()
{
    int t; scanf("%d", &t);
    while(t--)
    {
        scanf("%s%s", a, b);
        int len1 = strlen(a);
        int len2 = strlen(b);
        int i = 0, j = 0;
        while((i < len1) && (a[i] == a[0]))
            i++;
        while((j < len2) && (b[j] == b[0]))
            j++;
        if((i < j) || (a[0] != b[0]))
        {
            printf("No\n");
            continue;
        }
        i = j;
        for ( ; j < len2; ++j)
            if(i < len1 && (a[i] == b[j]))
                i++;
        if (i == len1 && j == len2)
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

你可能感兴趣的:(string)