2ms 348KB
有点小错误,忘记将last_j也初始化为0了。。刷不上去了,在评论里记录下吧。。
有点小错误,忘记将last_j也初始化为0了。。刷不上去了,在评论里记录下吧。。
我已经通过这道算法题! 问题描述:在计算机中,通配符一种特殊语法,广泛应用于文件搜索、数据库、正则表达式等领域。现要求各位实现字符串通配符的算法。 要求: 实现如下2个通配符: *:匹配0个或以上的字符(字符由英文字母和数...
https://gw-c.nowcoder.com/api/sparta/jump/link?link=https%3A%2F%2Fwww.nowcoder.com%2FquestionTerminal%2F43072d50a6eb44d2a6c816a283b02036
全部评论
#include <stdio.h>
#include <string.h>
int main(void)
{
int i, j, k, p, q, last_j, len_r, len_s;
char regular[188], s[888], record[888], ch, match;
for( ; scanf("%s%s", regular, s)!=EOF; ){
len_r = strlen(regular); len_s = strlen(s);
for(j=0; j<len_s; j++){
if((ch=s[j])>='a'&&ch<='z'){ record[j] = 1; continue; }
if(ch>='A'&&ch<='Z'){ s[j] = ch+32; record[j] = 1; continue; }
if(ch>='0'&&ch<='9'){ record[j] = 1; continue; }
record[j] = 0;
}
for(i=0; i<len_r; i++)
if((ch=regular[i])>='A' && ch<='Z') regular[i] = ch+32;
i = j = k = last_j = 0; //上榜代码这里忘记初始化last_j为0了!!
for( ; k<len_r; ){
if(regular[k] != '*'){ k++; continue; }
break;
}
if(i != k){
match = 0;
for( ; j<len_s; j++)
if(s[j]==regular[i] || regular[i]=='?'&&record[j]){
p = i+1; q = j+1;
for( ; p<k && q<len_s; p++,q++){
if(s[q] == regular[p]) continue;
if(regular[p]=='?' && record[q]) continue;
break;
}
if(p == k){ last_j = j = q; match = 1; break; }
}
if( !match ) goto opt;
}
if(k == len_r){ match = 1; goto opt; }
for(i=++k; k<len_r; i=++k){
for( ; k<len_r; ){
if(regular[k] != '*'){ k++; continue; }
break;
}
if(i != k){
match = 0;
for( ; j<len_s; j++)
if(s[j]==regular[i] || regular[i]=='?'&&record[j]){
p = i+1; q = j+1;
for( ; p<k && q<len_s; p++,q++){
if(s[q] == regular[p]) continue;
if(regular[p]=='?' && record[q]) continue;
break;
}
if(p == k){
for(p=j-1; p>=last_j; p--){
if( record[p] ) continue;
break;
}
if(p != last_j-1) goto opt;
last_j = j = q;
match = 1; break;
}
}
if( !match ) goto opt;
}
}
opt: printf("%s\n", match ? "true" : "false");
}
return 0;
}
相关推荐
昨天 17:28
深圳大学 测试开发 点赞 评论 收藏
分享

查看11道真题和解析