题目:不过多叙述,直接上思路🤭,相信来看题解的都是已经看过题得了。
题目说的“hehe”字符串不考虑方向,所以“eheh”也要判断,还需要合并每一列的字符在判断”hehe“和”eheh“在字符串里出现了多少次。(思路简单直接上代码)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
int count = 0;//统计数量
char ch[][]=new char[n][m];
String str[]=new String[n+m];//一共n+m个字符串
for (int i = 0; i < n; i++) {
str[i]=sc.next();
ch[i]=str[i].toCharArray();
}
// for(int i=0;i<n;i++) {
// for(int j=0;j<m;j++) {
// System.out.print(ch[i][j]);
// }
// System.out.println();
// }
for(int i=n;i<n+m;i++) {//统计每一列的字符组成的字符串
str[i]="";
for(int j=0;j<n;j++) {
str[i]+=ch[j][i-n];
}
// System.out.println(str[i]);
}
for(int i=0;i<n+m;i++) {
for(int j=0,k=4;k<=str[i].length();j++,k++) {
String temp=str[i].substring(j,k);
if("hehe".equals(temp)) {
count++;
}else if("eheh".equals(temp)) {
count++;
}
}
}
System.out.println(count);
}
}
各位,不懂可以私信但是相信各位都是大佬,🤭