UVa10010_Where's Waldorf?(小白书字符串专题)

来源:互联网 发布:chrome断点调试js 编辑:程序博客网 时间:2024/06/05 14:04

Description


  Where's Waldorf? 

Given a m by n grid of letters, ( $1 \leq m,n \leq 20$), and a list of words, find the location in the grid at which the word can be found. A word matches a straight, uninterrupted line of letters in the grid. A word can match the letters in the grid regardless of case (i.e. upper and lower case letters are to be treated as the same). The matching can be done in any of the eight directions either horizontally, vertically or diagonally through the grid.

Input 

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

The input begins with a pair of integers, m followed by n$1 \leqm,n \leq 50$ in decimal notation on a single line. The next m lines contain nletters each; this is the grid of letters in which the words of the list must be found. The letters in the grid may be in upper or lower case. Following the grid of letters, another integer k appears on a line by itself ( $1 \leq k \leq 20$). The next k lines of input contain the list of words to search for, one word per line. These words may contain upper and lower case letters only (no spaces, hyphens or other non-alphabetic characters).

Output 

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

For each word in the word list, a pair of integers representing the location of the corresponding word in the grid must be output. The integers must be separated by a single space. The first integer is the line in the grid where the first letter of the given word can be found (1 represents the topmost line in the grid, and m represents the bottommost line). The second integer is the column in the grid where the first letter of the given word can be found (1 represents the leftmost column in the grid, and n represents the rightmost column in the grid). If a word can be found more than once in the grid, then the location which is output should correspond to the uppermost occurence of the word (i.e. the occurence which places the first letter of the word closest to the top of the grid). If two or more words are uppermost, the output should correspond to the leftmost of these occurences. All words can be found at least once in the grid.

Sample Input 

18 11abcDEFGhigghEbkWalDorkFtyAwaldORmFtsimrLqsrcbyoArBeDeyvKlcbqwikomkstrEBGadhrbyUiqlxcnBjf4WaldorfBambiBettyDagbert

Sample Output 

2 52 31 27 8



解题报告

怀着小资的心情愉快的写下这篇。哈哈哈。。。

不容易呀,我可以说我都没时间做题么。。。

昨晚熬夜打算破题的,思路有了,就下手了,不过还是英语惹的货,一些细节的方面不注意WA了好几次。。。

我的思路是先把字符串二维数组网格str1建成,然后就是找第一个字符相同的地方,if(str1[i][j]==str2[0]),找完就开始用数组储存八个方向的字符串,大小同str2,然后就是检索相同字符串。。。

思路很好哈,熬夜不成功给睡去了。。。

第二天继续。。。

案例轻松过了,结果CE,尼玛神马情况,又是编译器的错,换成C++编译,就WA了。。。

哪里错了呢,原来还是急着破题,没看完题意,英语嘛,老不想看,题目最后写着 If two or more words are uppermost, the output should correspond to the leftmost of these occurences.意思就是如果两个以上符合输出,只输出最前面的。

解决了小问题又WA了,神马情况,一看,程序多了一句freopen(),赶紧删了在交,结果AC了。。。

心情真好哈。。。


#include<stdio.h>#include<string.h>#include<ctype.h>int main (){    //freopen("1.txt","r",stdin);    int i,j,p,t,n,m,k,l,q;    char str1[50][51],str2[51],c,str[8][51];    scanf("%d\n\n",&t);    while(t--)    {        scanf("%d %d\n",&m,&n);        /*for(i=0;i<m;i++)        {            scanf("%s",str1[i]);            for (j=0;j<strlen(str1[i]);j++)                str1[i][j]=tolower(str1[i][j]);        }*/        for(i=0;i<m;i++)            gets(str1[i]);        for(i=0;i<m;i++)            for(j=0;j<n;j++)                str1[i][j]=tolower(str1[i][j]);        scanf("%d\n",&k);        while(k--)        {            q=0;            gets(str2);            l=strlen(str2);            for(i=0;str2[i]!='\0';i++)                str2[i]=tolower(str2[i]);            for(i=0;i<m;i++)            {                for(j=0;j<n;j++)                {                    if(str1[i][j]==str2[0])                    {                        for(p=0;p<l;p++)//上                            str[0][p]=str1[i-p][j];                        str[0][p]='\0';                        for(p=0;p<l;p++)//下                            str[1][p]=str1[i+p][j];                        str[1][p]='\0';                        for(p=0;p<l;p++)//左                            str[2][p]=str1[i][j-p];                        str[2][p]='\0';                        for(p=0;p<l;p++)//右                            str[3][p]=str1[i][j+p];                        str[3][p]='\0';                        for(p=0;p<l;p++)//左上                            str[4][p]=str1[i-p][j-p];                        str[4][p]='\0';                        for(p=0;p<l;p++)//左下                            str[5][p]=str1[i+p][j-p];                        str[5][p]='\0';                        for(p=0;p<l;p++)//右上                            str[6][p]=str1[i-p][j+p];                        str[6][p]='\0';                        for(p=0;p<l;p++)//右下                            str[7][p]=str1[i+p][j+p];                        str[7][p]='\0';                        for(p=0;p<8;p++)                        {                            if(strcmp(str[p],str2)==0 && q==0)                            {                                printf("%d %d\n",i+1,j+1);                                q=1;                                break;                            }                            else continue;                        }                    }                }            }        }        if(t)printf("\n");    }    return 0;}


0 0