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