uva10010

字符串搜索  可以暴力搜 主要是代码要精简点  参考了网上的代码优化后写出来的

ac代码

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cctype>
 4 using namespace std;
 5 int m,n;
 6 int positionx;
 7 int positiony;
 8 char data[60][60];
 9 int dx[]={1,-1,0,0,-1,1,1,-1};
10 int dy[]={1,-1,1,-1,0,0,-1,1};
11 int ifcannext(int x,int y){
12     if(x>=0&&x<m&&y>=0&&y<n)
13      return 1;
14     else return 0;
15 }
16 void findposition(char input[]){
17     int i,j,k;
18     int inputlen=strlen(input);
19     for(i=0;i<m;i++){
20         for(j=0;j<n;j++){
21             if(data[i][j]!=input[0])
22             continue;
23             for(k=0;k<8;k++){
24                 char str[50];
25                 str[0]=data[i][j];
26                 int newx=i+dx[k];
27                 int newy=j+dy[k];
28                 int c=1;
29                 while(ifcannext(newx,newy)){
30                     str[c++]=data[newx][newy];
31                     if(c==inputlen) break;
32                     newx+=dx[k];
33                     newy+=dy[k];
34                 }
35                 str[c]='';
36                 if(strcmp(str,input)==0){
37                     positionx=i+1;
38                     positiony=j+1;
39                     return ;
40                 }
41             }
42         }
43     }
44     return ;
45 }
46 int main(){
47     int i,j;
48     int t=0;
49     int N;
50     cin>>N;
51     while(N--){
52         if(t!=0)
53          cout<<endl;
54         t++;
55         cin>>m>>n;
56         for(i=0;i<m;i++){
57             for(j=0;j<n;j++){
58                 cin>>data[i][j];
59                 data[i][j]=tolower(data[i][j]);
60             }
61         }
62         int t;
63         cin>>t;
64         while(t--){
65             char str[60];
66             cin>>str;
67             int len=strlen(str);
68             for(j=0;j<len;j++)
69                str[j]=tolower(str[j]);
70             findposition(str);
71             cout<<positionx<<" "<<positiony<<endl;
72         }
73     }
74     return 0;
75 }
原文地址:https://www.cnblogs.com/acplayfacm/p/3901690.html