472. Concatenated Words

本文详细解析了一道LeetCode竞赛题——找出所有能由给定字典中的单词拼接而成的字符串。通过排序和动态规划方法解决了内存溢出的问题,并提供了两种有效的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

周日参加了leetcode的contest,莫名其妙的被这道题卡住了,给出的反馈是内存超出,就觉得很郁闷,经过今天的好好研究终于发现了原因(根据错误样例反推原因)。
Given a list of words, please write a program that returns all concatenated words in the given list of words.

A concatenated word is defined as a string that is comprised entirely of at least two shorter words in the given array.

Example:
Input: [“cat”,”cats”,”catsdogcats”,”dog”,”dogcatsdog”,”hippopotamuses”,”rat”,”ratcatdogcat”]

Output: [“catsdogcats”,”dogcatsdog”,”ratcatdogcat”]

Explanation: “catsdogcats” can be concatenated by “cats”, “dog” and “cats”;
“dogcatsdog” can be concatenated by “dog”, “cats” and “dog”;
“ratcatdogcat” can be concatenated by “rat”, “cat”, “dog” and “cat”.
其实这道题,整体思路还是比较清晰的。要判断一个集合中的字符串能否被本集合中的其他字符串拼接而成,那么首先可以先对这个集合中的字符串按照字符长短的顺序进行排序(长的字符串只可能由比其更短的字符串拼接成)。故当我们排完序之后,在判断第i个词的时候,我们的dict里是由前面的i-1个词组成的。剩下的思路就 和word break几乎一样了。无非就是一些dp的思想去保存状态,这里我还是总结一下,给出两种常用的思想,供大家参考学习(直接用dfs搜索会导致超时)。

1 先直接上代码

bool com(const string a, const string b)
{
    return a.size() < b.size();
}
class Solution {
public:
    bool judge(string s, unordered_set<string>& wordDict,vector<bool>& dp,int before)
    {
        if(s.size() == 0)
            return true;
        else
        {
            for(int i=s.size()-1;i>=0;i--) //那个错误的样例就是这样逆序该对的
            {
                string a=s.substr(0,i+1);
                if(wordDict.find(a) != wordDict.end())
                {
                    string b=s.substr(i+1);
                    if(dp[i+1+before])
                    {
                        if(judge(b,wordDict,dp,before+i+1))
                            return true;
                        else
                            dp[i+1+before]=false;
                    }
                }
            }
            return false;
        }
    }
    vector<string> findAllConcatenatedWordsInADict(vector<string>& words) {
        vector<string> ans;
        unordered_set<string> dict;
        sort(words.begin(),words.end(),com);
        dict.insert(words[0]);   // 排完序之后,0是最短的字符串,不可能被其他的字符串拼接而成
        for(int i=1;i<words.size();i++)
        {
            vector<bool> dp(words.size()+1,true);
            if(judge(words[i],dict,dp,0))
                ans.push_back(words[i]);
            dict.insert(words[i]);
        }
        return ans;
    }
};

这种思路dp[i]保存的是判断从下标i开始一直到结尾的字符串是否能够被dict中的词语拼成。注意judge函数中的before变量,因为在每一次递归调用的时候当前字符串的部分前缀会被砍去,那么传到下一层的就是前缀被砍的字符串,那么对应的index是在原来总的字符串的位置,那么我们就需要记录之前被砍了多少的字符,以便获得正确的对应在原来字符串中的index。由于用到了递归函数的写法,故在一些特别变态的样例下会超时(因为递归调用的次数太多了,申请了过多的堆栈空间,我就是这样被莫名其妙卡住的)。根据错误样例把judge中的循环倒过来写就过了,从本质上来讲并没有简化多少,只不过针对这一批的测试样例罢了。

2 其实前面部分都是一样的,只是judge函数不一样。这里为了避免赘述,直接给出了judge函数。

bool judge(unordered_set<string>& wordDict, string s){
    vector<bool> dp(s.size()+1,false);
    dp[0] = true;
    for(int i = 1; i <= s.size(); i++)
    {
        for(int j = i - 1; j >= 0; j--)
        {
            if(dp[j])
            {
                string tmp = s.substr(j,i-j);
                if(wordDict.find(tmp) != wordDict.end())
                {
                    dp[i] = true;
                    break;
                }
            }
        }
    }

return dp[s.size()];
}
};

这里和上面的方法不同,dp[i]表示的是从字符串的头开始长度为i的字符串能否被dict中的词语拼成,由于i表示的是字符串的长度,故在初始化的时候dp的大小是s.size()+1。这种dp方法比较好理解,只需要注意字符串的小标就好了。

import pandas as pd import numpy as np from sklearn.preprocessing import MinMaxScaler, OneHotEncoder import tensorflow from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing.sequence import pad_sequences from tensorflow.keras.models import Model from tensorflow.keras.layers import Input, Dense, Embedding, LSTM, Concatenate, Dropout, BatchNormalization from tensorflow.keras.optimizers import Adam from sklearn.model_selection import train_test_split from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau # 1.数据预处理与特征工程 # 加载数据集 df = pd.read_csv("training_data.csv") # 数值特征标准化 num_features = ['position', 'quality'] scaler = MinMaxScaler() df[num_features] = scaler.fit_transform(df[num_features]) # 序列特征编码 tokenizer = Tokenizer(char_level=True, num_words=4) # 仅A,C,G,T四种碱基 tokenizer.fit_on_texts(df['context']) sequences = tokenizer.texts_to_sequences(df['context']) max_length = max(len(seq) for seq in sequences) padded_sequences = pad_sequences(sequences, maxlen=max_length, padding='post') # 标签提取 labels = df['label'].values # 2.双输入混合模型架构 # 序列输入分支 sequence_input = Input(shape=(max_length,), name='sequence_input') embedding = Embedding(input_dim=5, output_dim=8, input_length=max_length)(sequence_input) # 5=4碱基+填充 lstm_out = LSTM(32, return_sequences=False)(embedding) # 数值特征输入分支 numeric_input = Input(shape=(len(num_features),), name='numeric_input') dense_numeric = Dense(16, activation='relu')(numeric_input) bn_numeric = BatchNormalization()(dense_numeric) # 合并分支 concatenated = Concatenate()([lstm_out, bn_numeric]) dense1 = Dense(64, activation='relu')(concatenated) dropout1 = Dropout(0.3)(dense1) dense2 = Dense(32, activation='relu')(dropout1) output = Dense(1, activation='sigmoid')(dense2) # 构建模型 model = Model(inputs=[sequence_input, numeric_input], outputs=output) model.compile( optimizer=Adam(learning_rate=0.001), loss='binary_crossentropy', metrics=['accuracy', 'AUC'] ) model.summary() # 3.模型训练与评估 # 划分训练集和测试集 X_seq_train, X_seq_test, X_num_train, X_num_test, y_train, y_test = train_test_split( padded_sequences, df[num_features].values, labels, test_size=0.2, stratify=labels ) # 回调函数 callbacks = [ EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True), ReduceLROnPlateau(monitor='val_loss', factor=0.2, patience=5, min_lr=1e-6) ] # 训练模型 history = model.fit( [X_seq_train, X_num_train], y_train, validation_data=([X_seq_test, X_num_test], y_test), epochs=100, batch_size=64, callbacks=callbacks, class_weight={0: 1, 1: 2} # 处理类别不平衡 ) # 评估模型 test_loss, test_acc, test_auc = model.evaluate( [X_seq_test, X_num_test], y_test ) print(f"测试准确率: {test_acc:.4f}, AUC: {test_auc:.4f}") 请优化该代码 tensorflow.keras.preprocessing.text tensorflow.keras.preprocessing.sequence tensorflow.keras.models tensorflow.keras.layers tensorflow.keras.optimizers tensorflow.keras.callbacks 无法导入
最新发布
07-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值