我有一个dataDic,它是一个数组{"ant“、"bird”、"cat"}
dataDic是我想在stringPattern上搜索的单词数组
我想使用dataDic从stringPattern = birdantantcatbirdcat获得单词结果
Ex1。dataDic = {" ant“、" bird”、" cat "}答案是{bird、ant、ant、cat、bird、cat} Ex2。dataDic = {"ant","cat"}答案是{ant,ant,cat,cat}
这是我的代码‘私有静态字符串stringTest=“”birdantantcatbirdcat“;私有静态列表dicListWord;私有静态ListresultString =新ArrayList<>();
public static void main(String[] args) {
dicListWord = new ArrayList<>();
dicListWord.add("ant");
dicListWord.add("bird");
dicListWord.add("cat");
String[] data = stringTest.split("");
for (String dataDic:dicListWord) {
String [] wordList = dataDic.split("");
String foundWord = "";
for (String charTec:data) {
for (String dicWord:wordList) {
if(charTec.equals(dicWord)){
foundWord = foundWord.concat(charTec);
if(dataDic.equals(foundWord)){
resultString.add(foundWord);
foundWord = "";
}
}
}
}
}
for (String w1:data) {
for (String result:resultString) {
System.out.println(result);
}
}
}`///////////////////////////////////////////////////////////////////////////////
我运行的结果是
{蚂蚁、蚂蚁、鸟、鸟、蚂蚁、蚂蚁、鸟、蚂蚁、蚂蚁、鸟、鸟、蚂蚁,鸟,鸟,蚂蚁,蚂蚁,鸟,鸟}
发布于 2020-04-08 16:05:25
在导航字符串以查找单词匹配时,使用TreeMap存储单词作为键的位置和单词本身作为值的位置。您需要选择一个TreeMap的原因是,它是根据其键的自然顺序排序的,这是您的需求的一个重要方面。
您的要求是,结果列表中的单词应该按照它们在字符串中出现的顺序排列。
演示:
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<String> words = List.of("ant", "bird", "cat");
String str = "birdantantcatbirdcat";
System.out.println(getMatchingWords(words, str));
}
static List<String> getMatchingWords(List<String> words, String str) {
Map<Integer, String> map = new TreeMap<Integer, String>();
for (String word : words) {
Pattern pattern = Pattern.compile(word);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
map.put(matcher.start(), matcher.group());
}
}
return map.values().stream().collect(Collectors.toList());
}
}输出:
[bird, ant, ant, cat, bird, cat]发布于 2020-04-08 15:35:56
这是一个分词问题,可以用深度优先搜索来解决.但是,在给定的字符串模式是否可中断或没有获得更好的运行时间的情况下,检查给定的字符串模式是明智的,因为在这种情况下,我们已经给出了一个不匹配字典中任何单词的长字符串模式。
public class P00140_Word_Break_II {
public static void main(String[] args) {
String input = "catsanddog";
List<String> wordDict = Arrays.asList("cat", "cats", "and", "sand", "dog");
P00140_Word_Break_II solution = new P00140_Word_Break_II();
List<String> results = solution.wordBreak(input, wordDict);
System.out.println(results);
String input1 = "birdantantcatbirdcat";
List<String> wordDict1 = Arrays.asList("ant","bird","cat");
List<String> results1 = solution.wordBreak(input1, wordDict1);
System.out.println(results1);
}
public List<String> wordBreak(String s, List<String> wordDict) {
Set<String> dict = new HashSet<>(wordDict);
List<String> result = new ArrayList<>();
if (s == null || s.length() == 0 || !isbreakable(s, dict)) {
return result;
}
helper(s, 0, new StringBuilder(), dict, result);
return result;
}
public void helper(String s, int start, StringBuilder item, Set<String> dict, List<String> results) {
if (start >= s.length()) {
results.add(item.toString());
return;
}
if (start != 0) {
item.append(" ");
}
for (int i = start; i < s.length(); i++) {
String temp = s.substring(start, i + 1);
if (dict.contains(temp)) {
item.append(temp);
helper(s , i+1 , item , dict , results);
item.delete(item.length() + start - i - 1 , item.length());
}
}
if(start!=0) item.deleteCharAt(item.length()-1);
}
private boolean isbreakable(String s, Set<String> dict) {
boolean[] dp = new boolean[s.length() + 1];
dp[0] = true;
for (int i = 1; i <= s.length(); i++) {
for (int j = 0; j < i; j++) {
String subString = s.substring(j, i);
if (dp[j] && dict.contains(subString)) {
dp[i] = true;
break;
}
}
}
return dp[s.length()];
}}
https://stackoverflow.com/questions/61104221
复制相似问题