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