华为OD机试真题 C++ 实现【发现新词的数量 /新词挖掘/ 知识图谱/图谱新词挖掘1】

题目

题目描述:

小华负责公司知识图谱产品,现在要通过新词挖掘完善知识图谱新词挖掘: 给出一个待挖掘问题内容字符串Content和一人词的字符串word,找到content中所有word的新词。新词: 使用词word的字符排列形成的字符串。
请帮小华实现新词挖掘,返回发现的新词的数量。
输入描述
第一行输入为待挖掘的文本内容content;
第二行输入为词word;
输出描述
在content中找到的所有word的新词的数量

备注
0 ≤ content的长度 ≤10000000。

1 ≤ word的长度≤2000。

示例1:

输入
qweebaewqd
qwe

输出
2

说明
起始索引等于0的子串是“qwe”,它是word的新词起始索引等于6的子串是“ewg”,它是word的新词

示例2:

输入

abab
ab

输出
3

说明
起始索引等于0的子串是”ab“它是word的新词它是word的新词起始索引等于1的子串是”ba“起始索引等于2的子串是”ab“,它是word的新词

思路

1:类似leetcode76

滑动窗口

第一种滑动窗口
#include <bits/stdc++.h>
using namespace std;
int main() {
    string c,w;
    cin >> c >> w;
    int clen = c.size();
    int wlen = w.size();
    int res=0;
    map<char,int> mp2;
    for(int i=0;i<wlen;i++) {
        mp2[w[i]]++;
    }
    int left=0;
    map<char,int> mp1;
    for(int i=0;i<clen;i++) {
        mp1[c[i]]++;
        if(i-left+1==wlen) {
            int flag=1;
            for(auto m2:mp2) {
                if(mp1[m2.first]!=m2.second) {
                    flag=0;
                    break;
                }
            }
            if(flag){
                res++;
            }
            mp1[c[left++]]--;
        }
    }
    cout<<res<<endl;
    system("pause");
    return 0;
}

第二种排序  可能超时:
#include <iostream>
#include <algorithm>

using namespace std;

int findNewword(string&c, string& w) {
    int count = 0;
    for(int i = 0; i <= c.size()-w.size(); i++) {
        string str = c.substr(i, w.size());
		sort(str.begin(), str.end());
        if(str == w)) {
            count++;
        }
    }
    return count;
}

int main() {
    string c, w;
    cin >> c >> w;
	
    if(c.size() < w.size()) {
        cout << '0' << endl;
        return 0;
    }
	
    sort(w.begin(), w.end());
    int res = findNewword(c,w);
    cout << res << endl;
    system("pause");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值