@ 代码随想录算法训练营第8周(C语言)|Day51(动态规划)

@ 代码随想录算法训练营第8周(C语言)|Day51(动态规划)

Day51、动态规划(包含题目 139.单词拆分 )

139.单词拆分

题目描述

给定一个非空字符串 s 和一个包含非空单词的列表 wordDict,判定 s 是否可以被空格拆分为一个或多个在字典中出现的单词。

题目解答

char* substring(int j,int i,char*s){
   
    int len=i-j;
    char*ret=(char*)malloc(sizeof(char)*(len+1));
    for(int z=0;z<len;z++ ){
   
        ret[z]=s[++j];
    }
    ret[len]='\0';
    return ret;
}
bool comparestring(char **wordDict,char*s,int wordDictSize){
   
    for(int i=0;i<wordDictSize;i++){
   
        if(strcmp(wordDict[i],s)==0){
   
            return true;
        }
    }
    return false;
}

bool wordBreak(char* s, char** wordDict, int wordDictSize) {
   
    int len=strlen(s);
    int dp[len+1];
    char s1[len+1];
    for(int i=1,j=0;i<=len;i++){
   
        s1[i]=s[j++];
        dp[j]=0;
    }
    dp[0]=1;
    for(int i=1;i<=len;i++){
   
        for(int j=0;j<i;j++){
   
            char*ret=substring(j,i,s1);
            if(comparestring(wordDict,ret,wordDictSize)&&dp[j]==1){
   
                dp[i]=1;
            }
        }
    }

    if(dp[len]==1){
   
        return true;
    }
    return false;
}

题目总结

多重背包问题。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-18 09:40:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-18 09:40:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-18 09:40:02       82 阅读
  4. Python语言-面向对象

    2024-02-18 09:40:02       91 阅读

热门阅读

  1. 软件系统支持联营模式:实现共赢共利的关键

    2024-02-18 09:40:02       49 阅读
  2. 浅谈Websocket

    2024-02-18 09:40:02       54 阅读
  3. Mac更新node版本

    2024-02-18 09:40:02       47 阅读
  4. 函数作为参数传递和匿名函数(lambda)

    2024-02-18 09:40:02       48 阅读
  5. “AI文明的新纪元:从ChatGPT到Sora的跨越“

    2024-02-18 09:40:02       45 阅读
  6. 什么是tomcat?tomcat是干什么用的?

    2024-02-18 09:40:02       51 阅读
  7. spring boot Mybatis Plus分页

    2024-02-18 09:40:02       44 阅读