Problem B. Patterns Overlap
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, T. T 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
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.
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;
}