WPGGJ 2022-01-04 18:03 采纳率: 47.4%
浏览 39

java统计单词个数怎么使用map



```java
import java.util.Scanner;

public class Main{
    static int p;//行数
    static int k;//部分
    static String S;//字符串
    static int wordSize;
    static String word[];
    static int num = 0;
    static int cut;//分割

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        p = sc.nextInt();
        k = sc.nextInt();
        S = sc.nextLine();
        S = "";
        for(int i=0;i<p;i++){
            S = S + sc.nextLine();
        }
        wordSize = sc.nextInt();
        word = new String[wordSize];
        for(int j=0;j<wordSize;j++){
            word[j] = sc.next();
        }
        wordCount(S);
        if(cut<k) {
            num = num-(k-1);
        }
        System.out.println(num);
    }

    public static void wordCount(String S){
        for(int i=0;i<S.length();i++) {
            for(int j=0;j<wordSize;j++) {
                if(S.substring(i,S.length()).length()<word[j].length()) {
                    continue;
                }
                if(S.substring(i,i+word[j].length()).length()<word[j].length()){
                    continue;
                }
                if(S.substring(i,i+word[j].length()).equals(word[j])) {
                    //System.out.println(S+":"+word[j]);
                    num++;
                    //System.out.println("第"+num);
                    if(i!=0) {
                        cut++;
                        //System.out.println("分割:"+cut);
                    }
                    wordCount(S.substring(i+1,S.length()));
                    return;
                }
            }
        }
    }
}

```

  • 写回答

1条回答 默认 最新

  • 俺不理解 2022-01-04 18:11
    关注

    你的代码对吗?原始问题是什么?我现在不知道你的原始目的怎么帮你看代码

    评论

报告相同问题?

问题事件

  • 创建了问题 1月4日

悬赏问题

  • ¥15 CPU卡指令整合指令数据都在图片上
  • ¥15 火车票关联12306问题
  • ¥15 odoo17处理受托加工产品
  • ¥15 如何用MATLAB编码图三的积分
  • ¥15 圆孔衍射光强随孔径变化
  • ¥15 MacBook pro m3max上用vscode运行c语言没有反应
  • ¥15 ESP-PROG配置错误,ALL ONES
  • ¥15 结构功能耦合指标计算
  • ¥50 AI大模型精调(百度千帆、飞浆)
  • ¥15 非科班怎么跑代码?如何导数据和调参