算法:知识图谱新词挖掘

一、算法描述
小华负责公司知识图谱产品,现在要通过新词挖掘完善知识图谱。
新词挖掘:给出一个待挖掘文本内容字符串Content和一个词的字符串word,找到content中所有word的新词。
新词:使用词word的字符串排列形成的字符串。
请帮小华实现新词挖掘,返回发现的新词的数量。
输入描述:第一行输入待挖掘的文本内容content;
第二行输入为词word;
输出描述:在content中找到的所有word的新词的数量。
补充说明:0 <= content的长度 <= 10000000;
1 <= word的长度 <= 2000

示例1
输入:
qweebaewqd
qwe
输出:2
说明:起始索引等于0的子串是"qwe",它是word的新词。
起始索引等于6的子串是"ewq",它是word的新词。
示例2
输入:
abab
ab
输出:3
说明:起始索引等于0的子串"ab",它是 word的新词.
起始索引等于1的子串"ba",它是 word的新词。
起始索引等于2的字串"ab",它是 word的新词。
二、算法实现(Java)
public class NewWordFind {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String content = scanner.nextLine(); // abab
            String word = scanner.nextLine();    // ab
            System.out.println(newWordFind(content, word));
        }
    }

    public static int newWordFind(String content, String word) {
        int left = 0, right = 0; // 左右指针
        int wordCount = word.length();
        int total = 0; // 找到的新词的数量
        StringBuilder builder = new StringBuilder(); // 记录左右指针之间的字符串
        char[] wordArr = word.toCharArray();
        Arrays.sort(wordArr);                        // 对目标word排序
        while (right <= content.length() - 1) {
            builder.append(content.charAt(right));
            if (right - left + 1 == wordCount) {  // 左右之间字符数等于目标word字符数
                String sourceStr = builder.toString();    // ab
                char[] strArr = sourceStr.toCharArray();
                Arrays.sort(strArr);             // 排序
                if (Arrays.toString(strArr).equals(Arrays.toString(wordArr))) {
                    total++;
                }
                // 左移指针时,删除最左边字符
                List<Character> list = new ArrayList<>();
                for (char c : sourceStr.toCharArray()) {
                    list.add(c);
                }
                list.remove(0);
                // 将删除后的字符串重新放入builder,以便下次比较
                builder = new StringBuilder();
                list.forEach(builder::append);
                left++;
            }
            right++;
        }
        return total;
    }
}
三、运行结果

相关推荐

  1. 知识图谱数据库基本知识

    2024-02-01 10:24:03       22 阅读
  2. 数据挖掘|序列模式挖掘及其算法的python实现

    2024-02-01 10:24:03       38 阅读
  3. 【数据挖掘基础】数据挖掘技术概述和基本算法

    2024-02-01 10:24:03       25 阅读
  4. PyTorch知识图谱

    2024-02-01 10:24:03       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-01 10:24:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-01 10:24:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-01 10:24:03       82 阅读
  4. Python语言-面向对象

    2024-02-01 10:24:03       91 阅读

热门阅读

  1. 【无标题】

    2024-02-01 10:24:03       57 阅读
  2. cx_oracle连接oracle的us7ascii数据集乱码问题

    2024-02-01 10:24:03       53 阅读
  3. datax oracle->pg库 迁移表

    2024-02-01 10:24:03       60 阅读
  4. npm install -g pnpm 安装出现错误

    2024-02-01 10:24:03       53 阅读
  5. npm发布Vue组件

    2024-02-01 10:24:03       44 阅读
  6. Kubernetes实战(二十)-集群节点磁盘清理

    2024-02-01 10:24:03       51 阅读