Wildcard Matching
来源:互联网 发布:手机自动开启移动数据 编辑:程序博客网 时间:2024/05/16 16:18
/*from: http://www.jiuzhang.com/solutions/wildcard-matching/动态规划法dp[i][j]表示s的前i个字符与p的前j个字符匹配class Solution {public: bool isMatch(const char *s, const char *p) { if (s == NULL || p == NULL) { return false; } int n = strlen(s); int m = strlen(p); int f[n + 1][m + 1]; memset(f, false, sizeof(f)); f[0][0] = true; for (int i = 1; i <= n; i++) f[i][0] = false; for (int i = 1; i <= m; i++) f[0][i] = f[0][i - 1] && p[i - 1] == '*'; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (p[j - 1] == '*') { f[i][j] = f[i - 1][j] || f[i][j - 1]; } else if (p[j - 1] == '?') { f[i][j] = f[i - 1][j - 1]; } else { f[i][j] = f[i - 1][j - 1] && (s[i - 1] == p[j - 1]); } } } // for return f[n][m]; }};*/class Solution {public: bool isMatch(string s, string p) { int sLen = s.length(); int pLen = p.length(); if( sLen <= 0 || pLen <= 0 ) { return false; } bool dp[ sLen + 1 ][ pLen + 1 ]; memset( dp, false, sizeof(dp) ); dp[0][0] = true; for( int i = 1; i <= sLen; i++ ) { dp[i][0] = false; } for( int i = 1; i <= pLen; i++ ) { dp[0][i] = dp[0][ i - 1 ] && ( '*' == p[ i - 1 ] ); } for( int i = 1; i <= sLen; i++ ) { for( int j = 1; j <= pLen; j++ ) { if( '*' == p[ j - 1 ] ) { dp[i][j] = dp[i][ j - 1 ] || dp[ i - 1 ][j]; } else if( '?' == p[ j - 1 ] ) { dp[i][j] = dp[ i - 1 ][ j - 1 ]; } else { dp[i][j] = dp[ i - 1 ][ j - 1 ] && ( s[ i - 1 ] == p[ j - 1 ] ); } } } return dp[sLen][pLen]; }};//////////////////////////////////////////////////////////////////////////////////////*from: http://www.jiuzhang.com/solutions/wildcard-matching/动态规划法dp[i][j]表示s的前i个字符与p的前j个字符匹配class Solution {public: bool isMatch(const char *s, const char *p) { if (s == NULL || p == NULL) { return false; } int n = strlen(s); int m = strlen(p); int f[n + 1][m + 1]; memset(f, false, sizeof(f)); f[0][0] = true; for (int i = 1; i <= n; i++) f[i][0] = false; for (int i = 1; i <= m; i++) f[0][i] = f[0][i - 1] && p[i - 1] == '*'; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (p[j - 1] == '*') { f[i][j] = f[i - 1][j] || f[i][j - 1]; } else if (p[j - 1] == '?') { f[i][j] = f[i - 1][j - 1]; } else { f[i][j] = f[i - 1][j - 1] && (s[i - 1] == p[j - 1]); } } } // for return f[n][m]; }};*/class Solution {public: bool myMatch(const char *s, const char *p) { if (s == NULL || p == NULL) { return false; } int n = strlen(s); int m = strlen(p); vector< vector<bool> > f( n + 1, vector<bool>( m + 1, false ) ); //memset(f, false, sizeof(f)); f[0][0] = true; for (int i = 1; i <= n; i++) f[i][0] = false; for (int i = 1; i <= m; i++) f[0][i] = f[0][i - 1] && p[i - 1] == '*'; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (p[j - 1] == '*') { f[i][j] = f[i - 1][j] || f[i][j - 1]; } else if (p[j - 1] == '?') { f[i][j] = f[i - 1][j - 1]; } else { f[i][j] = f[i - 1][j - 1] && (s[i - 1] == p[j - 1]); } } } // for return f[n][m]; } bool isMatch(string s, string p) { return myMatch( s.c_str(), p.c_str() ); }};
0 0
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- Wildcard Matching
- bootstrap-----面板
- PS批量修改文件大小及类型
- [绍棠] iOS文件目录和文件操作 及NSFileManager的读写操作
- YARA字符串匹配
- Hadoop-hdfs
- Wildcard Matching
- Java自动读取指定文件夹下的所有文件
- C语言面试宝典
- 最简单的git命令
- libc
- php关于session应注意的事项
- 页面jsp编码utf-8,传递中文参数到java后台出现乱码
- HBase StoreFile(HFile)
- 一图理解行式数据库和列式数据库的区别