Kickstart Round A 2017 Problem.B Patterns Overlap 代补完

Problem B. Patterns Overlap

This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-Start Guide to get started.
Small input
13 points
Large input
22 points

Problem

Alice likes reading and buys a lot of books. She stores her books in two boxes; each box is labeled with a pattern that matches the titles of all of the books stored in that box. A pattern consists of only uppercase/lowercase English alphabet letters and stars (*). A star can match between zero and four letters. For example, books with the titles GoneGirl and GoneTomorrow can be put in a box with the pattern Gone**, but books with the titles TheGoneGirl, and GoneWithTheWind cannot.

Alice is wondering whether there is any book that could be stored in either of the boxes. That is, she wonders if there is a title that matches both boxes' patterns.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each consists of two lines; each line has one string in which each character is either an uppercase/lowercase English letter or *.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is TRUE if there is a string that matches both patterns, or FALSE if not.

Limits

1 ≤ T ≤ 50.

Small dataset

1 ≤ the length of each pattern ≤ 200.
Each pattern contains at most 5 stars.

Large dataset

1 ≤ the length of each pattern ≤ 2000.

Sample


Input 
 

Output 
 
3
****
It
Shakes*e
S*speare
Shakes*e
*peare

Case #1: TRUE
Case #2: TRUE
Case #3: FALSE


In sample case #1, the title It matches both patterns. Note that it is possible for a * to match zero characters.

In sample case #2, the title Shakespeare matches both patterns.

In sample case #3, there is no title that matches both patterns. Shakespeare, for example, does not work because the * at the start of the *peare pattern cannot match six letters.

题意:主人公,啊,假定为肉山吧(为啥总嘟嘟呢。。。)要整理书籍,现在她要对书进行归类。每本书的书名由字母和 “ * ” 组成,其中*号可以代替0~4个字符。比如GoneGirl 还有 GoneTomorrow是都能够分类到Gone**这个类别下的,然而像TheGoneGirl、 GoneWithTheWind这样的书就不行

输入的每组样例都含有两个书名,问它们是否能够相互匹配归到同一个类别下。

这题的小数据*号最多只有5个。。。所以我暴力枚举了一发。。。。然而正解肯定不是这样做的。所以等我看完正解再回来补完吧。

先把错误代码放上来留念一下。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int  maxn = 2010;
char s1[maxn],s2[maxn];
char afs1[4000][maxn],afs2[4000][maxn];
char now[maxn];
int num1,num2;
void dfs(int pos,int len,bool which,int p){
    int i,j,length;
    if(pos>=len){
        length = strlen(now);
        if(which){
            num1++;
            for(i=0;i<p;i++){
                afs1[num1][i] = now[i];
            }
            afs1[num1][p] = '\0';
        }else{
            num2++;
            for(i=0;i<p;i++){
                afs2[num2][i] = now[i];
            }
            afs2[num2][p] = '\0';
        }
        return;
    }
    if(which){
        if(s1[pos]=='*'){
            for(i=0;i<=4;i++){
                for(j=0;j<i;j++){
                    now[p+j] = ' ';
                }
                dfs(pos+1, len, true, p+j);
            }
        }else{
            now[p] = s1[pos];
            dfs(pos+1, len, true, p+1);
        }
    }else{
        if(s2[pos]=='*'){
            for(i=0;i<=4;i++){
                for(j=0;j<i;j++){
                    now[p+j] = ' ';
                }
                dfs(pos+1, len, false, p+j);
            }
        }else{
            now[p] = s2[pos];
            dfs(pos+1, len, false, p+1);
        }
    }
}
bool judge(int p1,int p2){
    int i,len1 = strlen(afs1[p1]),len2 = strlen(afs2[p2]);
    if(len1!=len2){
        return  false;
    }
    for(i=0;i<len1;i++){
        if(afs1[p1][i]==' '||afs2[p2][i]==' '){
            continue;
        }
        if(afs1[p1][i]!=afs2[p2][i]){
            return  false;
        }
    }
    return true;
}
int main() {
    freopen("in.txt","r",stdin);
    freopen("out.txt", "w", stdout);
    int t,i,j;
    scanf("%d",&t);
    getchar();
    int rnd = 1;
    while(t--){
        scanf("%s",s1);
        getchar();
        scanf("%s",s2);
        getchar();
        num1 = 0;
        dfs(0, strlen(s1), true, 0);
        num2 = 0;
        dfs(0, strlen(s2), false, 0);
        bool flag = false;
        for(i=1;i<=num1;i++){
            for(j=1;j<=num2;j++){
                if(judge(i, j)){
                    flag = true;
                }
            }
        }
        printf("Case #%d: ",rnd++);
        if(flag){
            printf("TRUE\n");
        }else{
            printf("FALSE\n");
        }
    }
    return 0;
}









评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值