当前位置:   article > 正文

leetcode+ Dp题目,字符串匹配_字符串匹配题dp

字符串匹配题dp
点击打开链接
  1. class Solution {
  2. public:
  3. bool isMatch(string s, string p) {
  4. int slen = s.length();
  5. int plen = p.length();
  6. int num = 0;
  7. for(int i=0; i<plen; i++){
  8. if(p[i]=='*') num+=1;
  9. }
  10. if(plen-num > slen) return false;
  11. vector<bool> pre(plen+1, false);
  12. pre[0] = true;
  13. for(int j=1; j<=plen; j++){
  14. pre[j] = pre[j-1] &&(p[j-1]=='*');
  15. }
  16. for(int i=1;i<=slen;i++){
  17. vector<bool> cur(plen+1, false);
  18. for(int j=1; j<=plen; j++){
  19. if(p[j-1] !='*'){
  20. cur[j] = pre[j-1]&&(s[i-1]==p[j-1] || p[j-1]=='?');
  21. }
  22. else{
  23. cur[j] = cur[j-1] || pre[j];
  24. }
  25. }
  26. pre = cur;
  27. }
  28. return pre[plen];
  29. }
  30. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/50738?site
推荐阅读
相关标签
  

闽ICP备14008679号