-
Notifications
You must be signed in to change notification settings - Fork 35
/
word_break.cpp
50 lines (46 loc) · 1.06 KB
/
word_break.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include<vector>
#include<set>
#include<cstring>
using namespace std;
int dp[301];
int help(int i,string s ,set<string>&wordDict){
if(i==s.size())
return 1;
string temp;
if(dp[i]!=-1)
return dp[i];
for(int j=i;j<s.size();j++){
temp+=s[j];
if(wordDict.find(temp)!=wordDict.end()){
if(help(j+1,s,wordDict)){
dp[i]=1;
return 1;
}
}
}
dp[i]=0;
return 0;
}
bool wordBreak(string s, vector<string>& wordDict) {
set<string>st;
memset(dp,-1,sizeof dp);
for(int i=0;i<wordDict.size();i++){
st.insert(wordDict[i]);
}
return help(0,s,st);
}
int main()
{
string s;
cin>>s;
vector<string>worddict;
int n;
cin>>n;string word;
for(int i=0;i<n;i++){
cin>>word;
worddict.push_back(word);
}
cout<<wordBreak(s,worddict);
return 0;
}