hdu-6170(模拟)

Two strings

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

Problem Description
Giving two strings and you should judge if they are matched.
The first string contains lowercase letters and uppercase letters.
The second string contains lowercase letters, uppercase letters, and special symbols: “.” and “*”.
. can match any letter, and * means the front character can appear any times. For example, “a.b” can match “acb” or “abb”, “a*” can match “a”, “aa” and even empty string. ( “*” will not appear in the front of the string, and there will not be two consecutive “*”.
 
Input
The first line contains an integer T implying the number of test cases. (T≤15)
For each test case, there are two lines implying the two strings (The length of the two strings is less than 2500).
 
Output
For each test case, print “yes” if the two strings are matched, otherwise print “no”.
 
Sample Input
3
aa
a* abb
a.* abb
aab
 
Sample Output
yes
yes
no
 
  题意:给2个字符串问这2个字符串是否匹配,第二个字符串有2个特殊字符“."和“*”,“."可以代表任何字符,“*”可以代表前一个字符可以出现的次数和位置不限,“*”不会出现在第一个,不会出现连续两个“*”。
  按题意模拟即可。
  
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<set>
#include<map>
#include<queue>
#include<vector>
#define ll long long int
#define INF 0x7fffffff
#define MAX 200005
#define me(a,b) memset(a,b,sizeof(a))
using namespace std;
int main()
{
    int t;
    set<char> sa,sb;
    char a[2505],b[2505];
    bool fa[2505];
    while(cin>>t)
    while(t--){
        cin>>a>>b;
        me(fa,false);
        sa.clear();
        sb.clear();
        int la=strlen(a),lb=strlen(b);
        int i=0,j=0,k=0;
        for(i=0;i<lb,j<la;i++){
            if(b[i]=='.'){
                if(b[i+1]=='*')
                    k++;
                else{
                    fa[j]=true;
                    j++;
                }
            }
            else if(b[i]=='*')
                continue;
            else{
                if(b[i+1]=='*')
                    sb.insert(b[i]);
                else{
                    while(b[i]!=a[j]&&j<la){
                        j++;
                    }
                    if(b[i]==a[j])
                        fa[j]=true;
                    j++;
                }
            }
        }
        bool flag=true;
        for(;i<lb;i+=2){
            if(b[i+1]!='*'){
                flag=false;
                break;
            }
            else{
                if(b[i]=='.')
                    k++;
                else
                    sb.insert(b[i]);
            }
        }
        if(!flag){
            cout<<"no"<<endl;
            continue;
        }
        for(i=0;i<la;i++){
            if(fa[i]==false){
                if(sb.find(a[i])==sb.end()){
                    if(k==0){
                        flag=false;
                        break;
                    }
                    else{
                        k--;
                        sb.insert(a[i]);
                    }
                }
            }
        }
        if(!flag)
            cout<<"no"<<endl;
        else
            cout<<"yes"<<endl;
    }
}

 

posted on 2018-01-27 17:20  珂朵莉  阅读(147)  评论(0编辑  收藏  举报

导航