标签:leetcode
https://oj.leetcode.com/problems/word-break-ii/
http://blog.csdn.net/linhuanmars/article/details/22452163
public class Solution {
public List<String> wordBreak(String s, Set<String> dict)
{
// Solution A:
// return wordBreak_NP(s, dict);
// Solution B:
return wordBreak_DP(s, dict);
}
///////////////////////////////
// Solution B: DP
//
public List<String> wordBreak_DP(String s, Set<String> dict)
{
s = "#" + s;
int len = s.length();
Solu[] solutions = new Solu[len];
// Init the first one;
solutions[0] = new Solu();
solutions[0].exist = true;
for (int i = 1 ; i < len ; i ++)
{
solutions[i] = new Solu();
for (int k = 0 ; k < i ; k ++)
{
if (solutions[k].exist)
{
String str = s.substring(k + 1, i + 1);
if (dict.contains(str))
{
solutions[i].exist = true;
solutions[i].froms.add(k);
}
}
}
}
// Build results
List<List<String>> sens = new ArrayList<List<String>>();
for (int i = 0 ; i < len ; i ++)
{
sens.add(new ArrayList<String>());
}
sens.get(0).add("");
buildResults(sens, solutions, len - 1, s);
return sens.get(len - 1);
}
private void buildResults(List<List<String>> sens, Solu[] solutions, int n, String s)
{
for (int i : solutions[n].froms)
{
if (sens.get(i).isEmpty())
buildResults(sens, solutions, i, s);
String newstr = s.substring(i + 1, n + 1);
for (String oldstr : sens.get(i))
{
sens.get(n).add((oldstr + " " + newstr).trim());
}
}
}
static class Solu
{
boolean exist;
List<Integer> froms = new ArrayList<>();
}
///////////////////////////////
// Solution A: NP
//
public List<String> wordBreak_NP(String s, Set<String> dict)
{
List<String> result = new ArrayList<>();
help(s, dict, "", 0, result);
return result;
}
private void help(String s, Set<String> dict, String cur, int start, List<String> result)
{
if (start >= s.length())
{
result.add(cur);
return;
}
for (int i = start ; i < s.length() ; i ++)
{
String temp = s.substring(start, i + 1);
if (dict.contains(temp))
{
String nstr = cur + " " + temp;
help(s, dict, nstr, i + 1, result);
}
}
}
}标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1600795