您的位置:首页 > 其它

UVA 10010 (暑假-字符串(2) -A - Where's Waldorf?)

2014-07-08 21:05 337 查看
#include <stdio.h>
#include <string.h>
#define  MAX 21
#define  MAX2 51

int main() {
int t;
scanf("%d", &t);
while (t--) {
//输入数据
char str[MAX2][MAX2], str2[MAX][MAX2];
int m, n;
scanf("%d%d", &m, &n);
getchar();
for (int i = 0; i < m; i++)
gets(str[i]);

int k;
scanf("%d", &k);
getchar();
for (int i = 0; i < k; i++)
gets(str2[i]);

// 把所有字母转换成小写
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
if ( str[i][j] < 97 )
str[i][j] += 32;

for (int i = 0; i < k; i++) {
int len = strlen(str2[i]);
for (int j = 0; j < len; j++)
if (str2[i][j] < 97)
str2[i][j] += 32;
}

//从字母表中寻找单词
for (int r = 0; r < k; r++ ) {  //从第一个单词开始
int len = strlen(str2[r]), kong = 0;
for (int i = 0; i < m && kong == 0; i++)
for (int j = 0; j < n && kong == 0; j++) { //单词的字母和字母表的字母匹配
int s2 = 0, x = i, y = j;
while  (j < n && str2[r][s2++] == str[i][j++] && kong == 0) { //向右匹配
if (s2 == len)
kong = 1;
}

s2 = 0,i = x, j = y;
while  (j >= 0 && str2[r][s2++] == str[i][j--] && kong == 0) { //向左匹配
if (s2 == len)
kong = 1;
}

s2 = 0,	i = x, j = y;
while  (i < m && str2[r][s2++] == str[i++][j] && kong == 0) {//向上匹配
if (s2 == len)
kong = 1;
}

s2 = 0,i = x, j = y;
while  ( i >= 0 && str2[r][s2++] == str[i--][j] && kong == 0) {//向下匹配
if (s2 == len)
kong = 1;
}

s2 = 0,i = x, j = y;
while  ( i < m && j < n && str2[r][s2++] == str[i++][j++] && kong == 0) {//向右上匹配
if (s2 == len)
kong = 1;
}

s2 = 0,	i = x, j = y;
while  ( i < m && j>= 0 &&str2[r][s2++] == str[i++][j--] && kong == 0) {//向右下匹配
if (s2 == len)
kong = 1;
}

s2 = 0,i = x, j =y;
while  ( i >= 0 && j >= 0 &&str2[r][s2++] == str[i--][j--] && kong == 0) {//向左下匹配
if (s2 == len)
kong = 1;
}

s2 = 0,i = x, j = y;
while  ( i >=  0 && j < n && str2[r][s2++] == str[i--][j++] && kong == 0) {//向左上匹配
if (s2 == len)
kong = 1;
}
s2 = 0,	i = x, j =y;

if (kong == 1) { //如果匹配成功就输出
printf("%d %d\n", x + 1, y + 1);
break;
}
}
}
if (t)
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: