您的位置:首页 > 其它

Leetcode---Word Break

2015-01-02 12:01 381 查看
Given a string s[/i] and a dictionary of words dict[/i], determine if s[/i] can be segmented into a space-separated sequence of one or more dictionary words.

For example, given

s[/i] = "leetcode",

dict[/i] = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

Have you been asked this question in an interview?

此题相当简单,动态规划

bool wordBreak(string s, unordered_set<string> &dict) {

int len=s.length();

vector<bool> dp(len+1,false);

dp[0]=true;

for(int i=0;i<=len;i++){

for(int j=0;j<i;j++){

if(dict.find(s.substr(j,i-j))!=dict.end() && dp[j]==true){

dp[i]=true;

}

}

}

return dp[len];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: