面试题 17.13. 恢复空格(动态规划+ 前缀后缀树)

在这里插入图片描述
居然超时了

class Solution {
public:
    bool ismatch( string s,vector<string>&dictionary ){
        vector<string>::iterator it = find(dictionary.begin(), dictionary.end(),s);
        return it!=dictionary.end();
    }
    int respace(vector<string>& dictionary, string sentence) {
        int n=sentence.size();
        vector<int> dp(n+1,0);
       string str="";
  
        int idx;
        for(int i=1;i<=n;i++){
                    
            dp[i]=dp[i-1]+1;
            string s="";
            for( idx=i-1;idx>=0;idx--){
                s+=sentence[idx];
                reverse(s.begin(),s.end());
               
                if(ismatch(s,dictionary)){
                    dp[i]=min(dp[i],dp[idx]);
                 
                } 
                reverse(s.begin(),s.end());

            }
   
        }
        // cout<<dp[23]<<endl;
        return dp[n];
    }
};
class Solution {
public:
    bool ismatch( string s,vector<string>&dictionary ){
        vector<string>::iterator it = find(dictionary.begin(), dictionary.end(),s);
        return it!=dictionary.end();
    }
    int respace(vector<string>& dictionary, string sentence) {
        int n=sentence.size();
        vector<int> dp(n+1,0);
       string str="";
  
        int idx;
        for(int i=1;i<=n;i++){
                    
            dp[i]=dp[i-1]+1;
          
            for( idx=0;idx<=i-1;idx++){
                
                if(find(dictionary.begin(),dictionary.end(),sentence.substr(idx,i-idx))!=dictionary.end()){
                    dp[i]=min(dp[i],dp[idx]);
                 
                } 
            

            }
   
        }
        // cout<<dp[23]<<endl;
        return dp[n];
    }
};
class Solution {
    public int respace(String[] dictionary, String sentence) {
       
        Trie trie=new Trie();
        for(String word: dictionary){
            trie.add(word);
        }
        int n = sentence.length();
        int[] dp = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            dp[i] = dp[i - 1] + 1;
            for (int idx :trie.search(sentence,i-1)) {
                    dp[i] = Math.min(dp[i], dp[idx]);
            }
        }
        return dp[n];
    }
};
class Node{
        public boolean isWord;
        public TreeMap<Character,Node>next; //多叉树
        public Node(){
            this.isWord=false;
            next=new TreeMap<>();
        }
    }
class Trie{
    private Node root;
    public Trie(){
        root=new Node();}
    public void add(String word){
        Node cur=root;
        for(int i=word.length()-1;i>=0;i--){
            char c=word.charAt(i);
            if(cur.next.get(c)==null) cur.next.put(c,new Node());
            cur=cur.next.get(c);
        }
        if(!cur.isWord){
                cur.isWord=true;
            }
    } // 找到 sentence 中以 endPos 为结尾的单词,返回这些单词的开头下标

    public List<Integer> search(String sentence, int endPos){
        List<Integer> indices=new ArrayList<> ();
        Node cur=root;
        for(int i=endPos;i>=0;i--){
            char c=sentence.charAt(i);
            if(cur.next.get(c)==null) break;
            cur=cur.next.get(c);
            if(cur.isWord) indices.add(i);
        }
        return indices;
    }
    
}

猜你喜欢

转载自blog.csdn.net/qq_38662930/article/details/107243916