# Cross Words

Cross Words

Time Limit:10000MS  Memory Limit:65536K
Total Submit:8 Accepted:5

Description

Cross words is such a famous game that we may meet at any corner. Many people dive into the game, including Leilei. But, as everyone knows, Leilei is poor in English, especially in his small size of vocabulary. And soon he finds that some puzzle is so hard to solve because he don’t know some of the key words to be filled in. So now when he gets the solution of one puzzle, he only wonders if the words are out of his knowledge. And as he is too lazy, he leaves the work to you.
A solution of the game is a matrix of R*C characters from the set {‘A’-‘Z’,’*’}.
A word of the game is the consecutive captain letters of one row or one column. The word should be read from top to bottom or from left to right. The word should be “longest”, that means, there should be no more captain letters to the two ends of the word.
For example, here is one puzzle.
*IS**
**CAN
**A**
**N**
The words from the rows are:
IS
CAN
A
N
The words from the column are:
I
SCAN
A
N
So if Leilei don’t know any of the eight words above, he can’t solve the puzzle forever.

Input

At the beginning of the input file, there is the vocabulary of Leilei.
The first line consists of one integer N (0<= N < 16384), which is the size of the vocabulary.
Each of the following N lines consists of one English word. This word is only made up with captain letters ‘A’-‘Z’. No word will be longer than 100 characters.
The first line of each test case there is two positive integers R and C. 0In the following R lines, each consists of C characters, from the set {‘A’-‘Z’,’*’}.
A line with R=C=0 indicating the end of the input file.

Output

For each test case, output one line consists of one word “YES” if Leilei can solve the puzzle or “NO” if not.

Sample Input

6
IS
CAN
I
SCAN
A
N
4 5
*IS**
**CAN
**A**
**N**
4 5
*IS**
**CAN
**AN*
**N**
0 0


Sample Output

YES
NO


Hint

*THIS*IS*ONE*LINE*WITH*SEVEN*WORDS*

#include<iostream>
using namespace std;
struct node
{
char a[110];
}nod[16390];
int cmp(const void *c,const void *b)
{
return strcmp( ((node*)c)->a,((node*)b)->a );
}
char a[120][120];
char text[120];
int main()
{
int N,i,j,R,C,temp,temp1,jump1,yes,jump2;

scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%s",nod[i].a);
}
qsort(nod,N,sizeof(nod[0]),cmp);
while(scanf("%d%d",&R,&C)!=EOF)
{
if(R==0&&C==0)
break;
yes=-1;

for(i=0;i<R;i++)
{
getchar();
for(j=0;j<C;j++)
{
scanf("%c",&a[i][j]);
}
}
i=0;
j=0;
for(i;i<R;i++)
{

j=0;
for(j;j<C;j++)
{
temp=0;
if(a[i][j]!='*')
{
yes=0;
text[temp++]=a[i][j];
j++;
while(a[i][j]!='*'&&j<C)
{

text[temp++]=a[i][j];
j++;
}
text[temp]='/0';
if(bsearch(text,nod,N,sizeof(nod[0]),cmp)==NULL)
{
yes=1;
goto jump1;
}

j--;

}
}
}
jump1:;
if(yes==0||yes==-1)
{
i=0;
j=0;
for(i;i<C;i++)
{

j=0;
for(j;j<R;j++)
{
temp=0;

if(a[j][i]!='*')
{
yes=0;
text[temp++]=a[j][i];
j++;
while(a[j][i]!='*'&&j<R)
{

text[temp++]=a[j][i];
j++;

}
text[temp]='/0';
if(bsearch(text,nod,N,sizeof(nod[0]),cmp)==NULL)
{
yes=1;
goto jump2;
}
j--;
}

}
}
jump2:;
}
if(yes==0)
printf("YES/n");
else
printf("NO/n");
}
return 0;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：Cross Words 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)