hdu5414 CRB and String(构造,模拟)

14 篇文章 0 订阅
2 篇文章 0 订阅

题目:

CRB and String

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


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
 

Recommend
wange2014

题意:给两个字符串s和t,可以在字符串s的任意字符后面添加和前一个字符不相同的字符,问s能不能转化为t。

思路:由于只能添加和前一个字符不同的字符,所以s和t的开始的字符必须相同并且s的起始连续的相同字符个数要大于等于t的,不然只能通过添加和前一个字符相同的字符来使s等于t。还有一个限制条件是s必须是t的子序列,也就是说s中出现的字符必须在t中出现而且s的字符的位置还要小于等于t中相应字符的位置,因为添加字符只能把字符向后移不能向前移。

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/



char s[100000+5],t[100000+5];
int cnt1[26],cnt2[26];
int main()
{int T;
RI(T);
while(T--)
{
    scanf("%s",s);
    scanf("%s",t);
    if(!strcmp(s,t))
    {
        printf("Yes\n");
        continue;
    }
    if(strlen(s)>=strlen(t)&&strcmp(s,t)!=0)
    {
        printf("No\n");
        continue;
    }
    int len1=strlen(s);
    int len2=strlen(t);
    int ok=1;
    int num1=0,num2=0;
    for(int i=0;i<len1;i++)
        {
            num1++;
            if(i<len1-1&&s[i]!=s[i+1])
                break;
        }
    for(int i=0;i<len2;i++)
    {
        num2++;
        if(i<len2-1&&t[i]!=t[i+1])
            break;
    }
    if(num2>num1)
    {
        printf("No\n");
        continue;
    }
    if(t[0]!=s[0])
    {
        printf("No\n");
        continue;
    }
    MS0(cnt1);
    MS0(cnt2);
   int i;
   int cur=0;
   for(i=0;i<len2;i++)
    {
        if(t[i]==s[cur])
            cur++;
    }
    if(cur!=len1)
    {
        ok=0;

    }

    if(ok)
    {
        printf("Yes\n");
    }
    else
        printf("No\n");
}



        return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值