2023华为OD机试真题-知识图谱新词挖掘(JAVA、Python、C++)

题目描述:
小华负责公司知识图谱产品,现在要通过新词挖掘完善知识图谱。
新词挖掘:给出一个待挖掘文本内容字符串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的新词。
 

import java.util.*;
 
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String content=in.nextLine();
        String word=in.nextLine();
        HashMap<Character,Integer> map = new HashMap<Character,Integer>();
        for(int i=0;i<word.length();i++){
            Character ch = word.charAt(i);
            if(map.get(ch)!=null){
                map.put(ch,map.get(ch)+1);
            }else{
                map.put(ch,1);
            }
        }
 
        int res = 0;
        for(int i=0;i<content.length();i++){
            int length = 0;
            HashMap<Character,Integer> useMap = (HashMap<Character,Integer>)map.clone();
            for(int j=i;j<content.length();j++){
                Character ch = content.charAt(j);
                Integer count = useMap.get(ch);
 
                if(count!=null && count!=0){
                    useMap.put(ch,count-1);
                    length++;
                    if(length == word.length()){
                        res++;
 
                        break;
                    }
                }else{
                    break;
                }
 
            }
 
 
        }
        System.out.println(res);
    }
 
}
import sys
import itertools
 
content = sys.stdin.readline().strip()
word = sys.stdin.readline().strip()
word_len = len(word)
 
words = [''.join(res) for res in itertools.permutations(word, word_len)]
 
n = 0
for i, s in enumerate(content):
    if content[i: i + word_len] in words:
        n += 1
        
print(n)
#include<iostream>
#include<set>
#include<string>
using namespace std;
 
int main() {
 string str;
 cin >> str;
 string word;
 cin >> word;
 int count = 0;
 multiset<char> wo;
 for (auto i : word)
  wo.insert(i);
 for (int i = 0; i < str.size() - word.size() + 1; ++i) {
  string temp = str.substr(i, word.size());
  multiset<char> st;
  for (auto i : temp) {
   st.insert(i);
  }
  if (st == wo)
   count++;
 }
 cout << count;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值