Redian新闻
>
囧问:好像aa的点数合并us airway后莫名奇妙的少了好几万。
avatar
囧问:好像aa的点数合并us airway后莫名奇妙的少了好几万。# Money - 海外理财
p*3
1
//DP矩阵中如果遇到.*或.+, 那么DP[i][j] == DP[i][j+1] = true/false
const int M = 100;
bool isMatchDP(const char* str, const char* pattern)
{
if (NULL == str || NULL == pattern)
return false;
int len1 = strlen(str);
int len2 = strlen(pattern);
bool DP[M][M] = { false }; //DP[len1+1][len2+1]
DP[0][0] = true;
for (int i = 1; i <= len2; )
{
if (pattern[i] == '*')
{
DP[0][i] = DP[0][i-1];
DP[0][i+1] = DP[0][i];
i += 2;
}
else i += 1;
}
for (int i = 1; i <= len1; i++)
{
for (int j = 1; j <= len2;)
{
if (pattern[j] != '*' && pattern[j] != '+')
{
DP[i][j] = (((str[i-1] == pattern[j-1]) || pattern[j-1] == '
.') && DP[i-1][j-1]);
j++;
}
else
{
int iter = i;
if (pattern[j] == '+')
{
if (pattern[j-1] != '.' && pattern[j-1] != str[i-1])
{
j += 2;
continue;
}
else iter--;
}
do
{
if (DP[iter][j-1])
{
DP[i][j] = true;
DP[i][j+1] = true;
break;
}
}
while (iter > 0 && str[iter-- - 1] == pattern[j-1]);
j += 2;
}
}
}
return DP[len1][len2];
}
============================
以前一直觉得很难,发现也不是太难写
avatar
z*1
2
怎么版?
avatar
x*0
3
mark
avatar
V*s
4
查activity,打客服电话

【在 z********1 的大作中提到】
: 怎么版?
avatar
h*i
5
顶id。。。。

【在 p*****3 的大作中提到】
: //DP矩阵中如果遇到.*或.+, 那么DP[i][j] == DP[i][j+1] = true/false
: const int M = 100;
: bool isMatchDP(const char* str, const char* pattern)
: {
: if (NULL == str || NULL == pattern)
: return false;
: int len1 = strlen(str);
: int len2 = strlen(pattern);
: bool DP[M][M] = { false }; //DP[len1+1][len2+1]
: DP[0][0] = true;

avatar
w*n
6
我的也是 拖延症犯了一直没有来得及打电话
avatar
h*0
7
目测是八百题大牛的马甲。。
avatar
c*p
8
mark
avatar
J*3
9
顶!头像也很稀饭!
avatar
h*u
10
mark
avatar
p*3
11

真有眼光,
头像是精心挑选的!

【在 J****3 的大作中提到】
: 顶!头像也很稀饭!
avatar
e*n
12
我也喜欢这个头像!
avatar
j*x
13
这是2爷的新id?
avatar
k*j
14
贴一个从后往前的DPcode。与lz的解法大同小异
class Solution {
public:
bool isMatch(const char *s, const char *p) {
// Start typing your C/C++ solution below
// DO NOT write int main() function

int len1 = strlen(s);
int len2 = strlen(p);
int i,j;
vector > result(len1+1,vector (len2+1,0));
result[len1][len2] = 1;
for(j = len2-1;j>=0;j--){
if(*(p+j+1)=='*'){
result[len1][j] = result[len1][j+2];
}
}
for(i = len1-1;i>=0;i--){
for(j = len2-1;j>=0;j--){
if(*(p+j)=='.'){
if(*(p+j+1)=='*'){
result[i][j] = result[i+1][j];
if(!result[i][j] && j+2<=len2)
result[i][j] = result[i][j+2];
}
else
result[i][j] = result[i+1][j+1];
}
else if(*(p+j)=='*')
result[i][j] = 0;
else{
if(*(p+j)=='*')
result[i][j] = result[i][j+2] || *(p+j)==*(s+i) && result[
i+1][j];
else
result[i][j] = *(p+j)==*(s+i) && result[i+1][j+1];
}
}
}
return result[0][0];
}
};
avatar
d*n
15
Just curious, what if there is '*' in the s, does it still work?

【在 k*j 的大作中提到】
: 贴一个从后往前的DPcode。与lz的解法大同小异
: class Solution {
: public:
: bool isMatch(const char *s, const char *p) {
: // Start typing your C/C++ solution below
: // DO NOT write int main() function
:
: int len1 = strlen(s);
: int len2 = strlen(p);
: int i,j;

avatar
d*n
16
public class Solution {
public boolean isMatch(String s, String p) {
if (p=="") return s == "";

int m = s.length() + 1;
int n = p.length() + 1;
boolean[][] result = new boolean[n][m];

// Initialization part
result[0][0] = true;
result[1][0] = false;

for( int i = 2; i < n; i++)
result[i][0] = p.charAt(i-1) == '*' ? result[i-2][0] : false;

for( int j = 1; j < m; j++){
result[0][j] = false;
result[1][j] = false;
}

if (m > 1)
result[1][1] = match(s.charAt(0), p.charAt(0));

// Major DP part
for( int i = 2; i < n; i++ ){
for( int j = 1; j < m; j++ ){
if(p.charAt(i-1) == '*')
result[i][j] = match(s.charAt(j-1),p.charAt(i-2))?
(result[i][j-1]||result[i-2][j]) : result[i-2][j];
else
result[i][j] = match(s.charAt(j-1),p.charAt(i-1))?
result[i-1][j-1] : false;
}
}
return result[n-1][m-1];
}

boolean match(char a, char b){
return (a == b || b=='.');
}
}
avatar
d*n
17
Another one for Wildcard match:
public class Solution {
public boolean isMatch(String s, String p) {
int m = s.length() + 1;
int n = p.length() + 1;
boolean[][] result = new boolean[n][m];

result[0][0] = true;
for( int i = 1; i < n; i++){
result[i][0] = p.charAt(i-1) == '*' ? result[i-1][0] : false;
}
for( int j = 1; j < m; j++){
result[0][j] = false;
}
for( int i = 1; i < n; i++ ){
for( int j = 1; j < m; j++ ){
if(p.charAt(i-1) == '*'){
result[i][j] = result[i-1][j]|result[i-1][j-1]|result[i][j-1];
} else
result[i][j] = match(s.charAt(j-1),p.charAt(i-1)) ?
result[i-1][j-1] : false;
}
}
return result[n-1][m-1];
}

boolean match(char a, char b){
return (a == b || b=='?');
}
}

【在 d****n 的大作中提到】
: public class Solution {
: public boolean isMatch(String s, String p) {
: if (p=="") return s == "";
:
: int m = s.length() + 1;
: int n = p.length() + 1;
: boolean[][] result = new boolean[n][m];
:
: // Initialization part
: result[0][0] = true;

avatar
c*o
18
顶,晚上来学习

★ 发自iPhone App: ChineseWeb 7.8

【在 p*****3 的大作中提到】
: //DP矩阵中如果遇到.*或.+, 那么DP[i][j] == DP[i][j+1] = true/false
: const int M = 100;
: bool isMatchDP(const char* str, const char* pattern)
: {
: if (NULL == str || NULL == pattern)
: return false;
: int len1 = strlen(str);
: int len2 = strlen(pattern);
: bool DP[M][M] = { false }; //DP[len1+1][len2+1]
: DP[0][0] = true;

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。