LeetCode-Word Break-断词

https://oj.leetcode.com/problems/word-break/

水题。简单DP。

class Solution {
public:
    int n,m;
    bool wordBreak(string s, unordered_set<string> &dict) {
        n=s.length();
        vector <bool> dp(n+1,0);
        dp[0]=true;
        for (int i=1;i<=n;i++){
            for (int j=0;j<i;j++){
                string cur=s.substr(j,i-j);
                if (dict.find(cur)!=dict.end()){
                    if (dp[j]){
                        dp[i]=true;
                        break;
                    }
                }
            }
        }
        return dp[n];
    }
};

  

posted @ 2014-10-07 18:00  zombies  阅读(98)  评论(0编辑  收藏  举报