网易互娱的笔试-关于字符匹配的题目

#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool up_test(vector<string> input,string haha,int rows,int cols)
{     for(int i=0;i<haha.size();i++)     {         if(input[rows-i][cols]!=haha[i])             return false;     }     return true;
}
bool down_test(vector<string> input,string haha,int rows,int cols)
{     for(int i=0;i<haha.size();i++)     {         if(input[rows+i][cols]!=haha[i])             return false;     }     return true;
}
bool left_test(vector<string> input,string haha,int rows,int cols)
{     for(int i=0;i<haha.size();i++)     {         if(input[rows][cols-i]!=haha[i])             return false;     }     return true;
}
bool right_test(vector<string> input,string haha,int rows,int cols)
{     for(int i=0;i<haha.size();i++)     {         if(input[rows][cols+i]!=haha[i])             return false;     }     return true;
}
bool up_left_test(vector<string> input,string haha,int rows,int cols)
{     for(int i=0;i<haha.size();i++)     {         if(input[rows-i][cols-i]!=haha[i])             return false;     }     return true;
}
bool up_right_test(vector<string> input,string haha,int rows,int cols)
{     for(int i=0;i<haha.size();i++)     {         if(input[rows-i][cols+i]!=haha[i])             return false;     }     return true;
}
bool down_left_test(vector<string> input,string haha,int rows,int cols)
{     for(int i=0;i<haha.size();i++)     {         if(input[rows+i][cols-i]!=haha[i])             return false;     }     return true;
}
bool down_right_test(vector<string> input,string haha,int rows,int cols)
{     for(int i=0;i<haha.size();i++)     {         if(input[rows+i][cols+i]!=haha[i])             return false;     }     return true;
}
int main()
{     vector<string> test;     int test_num=0;     int rows;     int cols;     int count=0;     vector<int> outall;     string test_string;     cin>>test_num;     for(int i=0;i<test_num;i++)     {         test.resize(0);         cin>>rows>>cols;         test.resize(rows);         count=0;         for(int j=0;j<rows;j++)         {             cin>>test[j];         }         cin>>test_string;         for(int x=0;x<rows;x++)             for(int y=0;y<cols;y++)             {                 if(test[x][y]==test_string[0])                 {                     if(y>=test_string.size())                     {                         ;//count=count+(int)left_test(test,test_string,x,y);                     }                     if(y<=(cols-test_string.size()))                     {                         count=count+(int)right_test(test,test_string,x,y);                     }                     if(x>=test_string.size())                     {                         ;//count=count+(int)up_test(test,test_string,x,y);                     }                     if(x<=(rows-test_string.size()))                     {                         count=count+(int)down_test(test,test_string,x,y);                     }                     if(y>=test_string.size()&&x>=test_string.size())                     {                         ;//count=count+(int)up_left_test(test,test_string,x,y);                     }                     if(y>=test_string.size()&&x<=(rows-test_string.size()))                     {                         ;//count=count+(int)down_left_test(test,test_string,x,y);                     }                     if(y<=(cols-test_string.size())&&x>=test_string.size())                     {                         ;//count=count+(int)up_right_test(test,test_string,x,y);                     }                     if(y<=(cols-test_string.size())&&x<=(rows-test_string.size()))                     {                         count=count+(int)down_right_test(test,test_string,x,y);                     }                 }             }             outall.push_back(count);     }     for(int i=0;i<test_num;i++)     {         cout<<outall[i]<<endl;     }     return 0;
}
/*
3
10 10
AAAAAADROW
WORDBBBBBB
OCCCWCCCCC
RFFFFOFFFF
DHHHHHRHHH
ZWZVVVVDID
ZOZVXXDKIR
ZRZVXRXKIO
ZDZVOXXKIW
ZZZWXXXKIK
WORD
3 3
AAA
AAA
AAA
AA
5 8
WORDSWOR
ORDSWORD
RDSWORDS
DSWORDSW
SWORDSWO
SWORD
*/
#网易#
全部评论
#include <cstdio> #include <cstdlib> #include <string> #include <iostream> #include <cstring> using namespace std; const int maxn = 100; enum DIR {     RIGHT,     LOW,     RLOW }; int dfs(char maze[maxn][maxn], int m, int n, string s, string find, int idx, int x, int y, DIR d) {     if (s == find) return 1;     if (idx >= find.size()) return 0;     if (s[idx] != find[idx]) return 0;     int right = 0, low = 0, rlow = 0;     if (d == RIGHT && y + 1 < n) {         right = dfs(maze, m, n, s + maze[x][y + 1], find, idx + 1, x, y + 1, RIGHT);     }     if (d == LOW && x + 1 < m) {         low = dfs(maze, m, n, s + maze[x + 1][y], find, idx + 1, x + 1, y, LOW);     }     if (d == RLOW && x + 1 < m && y + 1 < n) {         rlow = dfs(maze, m, n, s + maze[x + 1][y + 1], find, idx + 1, x + 1, y + 1, RLOW);     }     return right + low + rlow; } int main() {     int t;     char maze[maxn][maxn];     string find;     cin >> t;     while (t--) {         memset(maze, 0, sizeof(maze));         int m, n;         cin >> m >> n;         for (int i = 0; i < m; ++i) {             cin >> maze[i];         }         cin >> find;         int ans = 0;         for (int i = 0; i < m; ++i) {             for (int j = 0; j < n; ++j) {                 string s("");                 int r = dfs(maze, m, n, s + maze[i][j], find, 0, i, j, RIGHT);                 int l = dfs(maze, m, n, s + maze[i][j], find, 0, i, j, LOW);                 int rl = dfs(maze, m, n, s + maze[i][j], find, 0, i, j, RLOW);                 ans = ans + r + l + rl;             }         }         cout << ans << endl;     }     return 0; }
点赞 回复 分享
发布于 2017-09-16 21:46
刚开始以为是BFS,后来发现其实更简单
点赞 回复 分享
发布于 2017-09-16 21:47
我是用dfs,第一次搜索3个方向,第二次只搜索前一次的方向。。
点赞 回复 分享
发布于 2017-09-16 21:51
我直接遍历所有字符,然后分别从三个方向尝试探测下,也A了…… 不用DFS、BFS呀……
点赞 回复 分享
发布于 2017-09-16 22:01

相关推荐

冷艳的小师弟在看机会:jd测评乱点直接被挂了,哭死~
点赞 评论 收藏
分享
字节 飞书绩效团队 (n+2) * 15 + 1k * 12 + 1w
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务