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

#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

相关推荐

10-17 12:16
同济大学 Java
7182oat:快快放弃了然后发给我,然后让我也泡他七天最后再拒掉,狠狠羞辱他一把😋
点赞 评论 收藏
分享
Yushuu:你的确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了😆
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务