水题
直接暴力模拟
#include <iostream> #include <cstring> #include <algorithm> #include <string> using namespace std; int n, m; string a[55]; int main() { cin >> n >> m; for (int i = 1; i <= n; i ++ ) cin >> a[i]; int res = 0; int i = 1, j = 0; while(1) { string ch; ch = a[i].substr(j,2) + a[i+1].substr(j,2); //每次截取的四个字母组成的字符串 if(ch.find('f') != -1) if(ch.find('a') != -1) if(ch.find('c') != -1) if(ch.find('e') != -1) res ++; //字符串同时存在face四个字母时 答案++ //cout << ch << endl; j ++; //每次4格截取右移一位 if (j == m-1) { j = 0; i ++; } if (ch.size() != 4) //截取不足4字母时退出循环 break; } cout << res << endl; }