hdu 5414 CRB and String(2015 Multi-University Training Contest 10)

CRB and String

                                                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                                   Total Submission(s): 120    Accepted Submission(s): 37


Problem Description
CRB has two strings  s  and  t .
In each step, CRB can select arbitrary character  c  of  s  and insert any character  d  ( d  c ) just after it.
CRB wants to convert  s  to  t . But is it possible?
 

Input
There are multiple test cases. The first line of input contains an integer  T , indicating the number of test cases. For each test case there are two strings  s  and  t , one per line.
1 ≤  T  ≤  105
1 ≤  |s|  ≤  |t|  ≤  105
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
 

Author
KUT(DPRK)
 

Source
2015 Multi-University Training Contest 10
 


    解题思路:
        
       由于是在s串上进行操作,s必须包含在t串中,还有t串的第1个元素必须相等且连续出现的次数也必须小于等于
  
  s串的,其他的连续出现的就不需要考虑了,因为可以在连续出现之前一直添加,如样例4,可以在a后添加多少个
 
  p都没问题。


 代码:
 
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn=100000+100;
char s[maxn];
char ts[maxn];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%s",s,ts);
        int n1=strlen(s);
        int n2=strlen(ts);
        int cur=0;
        for(int i=0;i<n2;i++)
        {
            if(ts[i]==s[cur])
            cur++;
            if(cur==n1)
            break;
        }
        if(cur==n1)
        {
            int a1=1,a2=1;
            for(int j=1;j<n1;j++)
            {
                if(s[j]==s[0])
                a1++;
                else
                break;
            }
            for(int j=1;j<n2;j++)
            {
                if(ts[j]==ts[0])
                a2++;
                else
                break;
            }
            if(a2>a1||s[0]!=ts[0])
            printf("No\n");
            else
            printf("Yes\n");
        }
        else
        printf("No\n");
    }
    return 0;
}

你可能感兴趣的:(Algorithm,算法,ACM,HDU,多校)