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

来源:互联网 发布:淘宝网男装新款 编辑:程序博客网 时间:2024/05/01 07:23
#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;}


0 0
原创粉丝点击