TheAlgorithms-C-Plus-Plus/dynamic_programming/word_break.cpp

83 lines
2.2 KiB
C++
Raw Normal View History

2020-10-14 12:52:55 +08:00
/*
Given a non-empty string s and a dictionary wordDict containing a list of
non-empty words, determine if s can be segmented into a space-separated sequence
of one or more dictionary words.
Note:
The same word in the dictionary may be reused multiple times in the
segmentation. You may assume the dictionary does not contain duplicate words.
Example 1:
Input: s = "leetcode", wordDict = ["leet", "code"]
Output: true
Explanation: Return true because "leetcode" can be segmented as "leet code".
Example 2:
Input: s = "applepenapple", wordDict = ["apple", "pen"]
Output: true
Explanation: Return true because "applepenapple" can be segmented as "apple pen
apple". Note that you are allowed to reuse a dictionary word. Example 3:
Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
Output: false
*/
2020-10-14 13:08:22 +08:00
#include <limits.h>
2020-10-14 12:52:55 +08:00
#include <iostream>
#include <string>
#include <unordered_set>
#include <vector>
2020-10-14 13:08:22 +08:00
using std::cout;
using std::endl;
using std::string;
using std::unordered_set;
using std::vector;
2020-10-14 12:52:55 +08:00
class Solution {
public:
2020-10-14 13:08:22 +08:00
bool exists(const string s, const unordered_set<string> strSet) {
2020-10-14 12:52:55 +08:00
return strSet.find(s) != strSet.end();
}
2020-10-14 13:08:22 +08:00
bool check(const string s, const unordered_set<string> strSet, int pos,
2020-10-14 12:52:55 +08:00
vector<int>& dp) {
if (pos == s.length()) {
return true;
}
if (dp[pos] != INT_MAX) {
return dp[pos] == 1;
}
string wordTillNow = "";
for (int i = pos; i < s.length(); i++) {
wordTillNow += string(1, s[i]);
if (exists(wordTillNow, strSet) and check(s, strSet, i + 1, dp)) {
dp[pos] = 1;
return true;
}
}
dp[pos] = 0;
return false;
}
2020-10-14 13:08:22 +08:00
bool wordBreak(const string s, const vector<string>& wordDict) {
2020-10-14 12:52:55 +08:00
unordered_set<string> strSet;
2020-10-14 13:08:22 +08:00
for (const auto s : wordDict) {
2020-10-14 12:52:55 +08:00
strSet.insert(s);
}
vector<int> dp(s.length(), INT_MAX);
return check(s, strSet, 0, dp);
}
};
2020-10-14 13:08:22 +08:00
int main() {
const string s = "applepenapple";
const vector<string> wordDict = {"apple", "pen"};
cout << Solution().wordBreak(s, wordDict) << endl;
}