题主脑子真的很笨,C语言文科生水平(然鹅题主已经计算机专业大三了呀呀呀呀呀呀~~~),语言非常通俗易懂,就此分享一下自己今天de了一天bug的体会~~~~
这道题的解法网上已经有了,高手们可以参考https://blog.csdn.net/SunMoonVocano/article/details/76063748,(自己很开心的发现自己的解法的时间复杂度2更小一些~~~o(* ̄▽ ̄*)ブ)但是自己实在是看不懂,所以这里主要分享一下自己的思路以及debug艰辛历程,只放干货~~~
这道问题的描述:
It's hard times now. Today Petya needs to score 100 points on Informatics exam. The tasks seem easy to Petya, but he thinks he lacks time to finish them all, so he asks you to help with one..
There is a glob pattern in the statements (a string consisting of lowercase English letters, characters "?" and "*"). It is known that character "*" occurs no more than once in the pattern.
Also, n query strings are given, it is required to determine for each of them if the pattern matches it or not.
Everything seemed easy to Petya, but then he discovered that the special pattern characters differ from their usual meaning.
A pattern matches a string if it is possible to replace each character "?" with one good lowercase English letter, and the character "*" (if there is one) with any, including empty, string of bad lowercase English letters, so that the resulting string is the same as the given string.
The good letters are given to Petya. All the others are bad.
Input
The first line contains a string with length from 1 to 26 consisting of distinct lowercase English letters. These letters are good letters, all the others are bad.
The second line contains the pattern — a string s of lowercase English letters, characters "?" and "*" (1 ≤ |s| ≤ 105). It is guaranteed that character "*" occurs in s no more than once.
The third line contains integer n (1 ≤ n ≤ 105) — the number of query strings.
n lines follow, each of them contains single non-empty string consisting of lowercase English letters — a query string.
It is guaranteed that the total length of all query strings is not greater than 105.
Output
Print n lines: in the i-th of them print "YES" if the pattern matches the i-th query string, and "NO" otherwise.
You can choose the case (lower or upper) for each letter arbitrary.
Examples
Input
ab a?a 2 aaa aab
Output
YES NO
Input
abc a?a?a* 4 abacaba abaca apapa aaaaax
Output
NO YES NO YES
Note
In the first example we can replace "?" with good letters "a" and "b", so we can see that the answer for the first query is "YES", and the answer for the second query is "NO", because we can't match the third letter.
Explanation of the second example.
- The first query: "NO", because character "*" can be replaced with a string of bad letters only, but the only way to match the query string is to replace it with the string "ba", in which both letters are good.
- The second query: "YES", because characters "?" can be replaced with corresponding good letters, and character "*" can be replaced with empty string, and the strings will coincide.
- The third query: "NO", because characters "?" can't be replaced with bad letters.
- The fourth query: "YES", because characters "?" can be replaced with good letters "a", and character "*" can be replaced with a string of bad letters "x".
首先审题真的很重要呀~~~!!!!
很容易就不小心将“*”理解成了可以匹配任意字符,题主这头猪就是这样做的,因为题目中给出的测试样例的特殊性,自己检测的时候并没有发现,后来重新审题才发现.......
更改过来之后,算法是这样的:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
//全部变成小写的子函数
void converse(char str[]){
int len,i;
len=strlen(str);
for(i=0;i<len;i++){
if(('a'-str[i])>0){//bug1!!!!
str[i]=str[i]+32;
}
}
}
int main()
{
int i,j,k,p,m,num,spot=-1,len_good,len_bad,len_input,len_str,fail;
int yes[26]={0};
char good[50];
char bad[50];
char str[100];//记得改!!!因为题目里面要求的是10的5次方,这里测试,自己的电脑会跑爆掉~~~
char input[100][100];
int result[100];
//输入部分
gets(good);
converse(good);
gets(str);
converse(str);
scanf("%d",&num);
getchar();
len_good=strlen(good);
len_bad=26-strlen(good);
for(i=0;i<len_good;i++){//看看good数组里面有哪些字母
yes[(good[i]-'a')]=1;
}
for(i=0,j=0;i<26;i++){//找全bad数组
if(yes[i]==0){
bad[j]=(char)('a'+i);
j++;
}
}
len_str=strlen(str);
for(i=0;i<len_str;i++){
if(str[i]=='*')
spot=i;
}
for(i=0;i<num;i++){//根据多少行来计算的
gets(input[i]);
printf("%s",input[i]);
printf("%d",i);
converse(input[i]);
printf("%s",input[i]);
printf("%d",i);
len_input=strlen(input[i]);
if(((spot<0)&&(len_input!=len_str))||((spot>=0)&&((len_str-len_input)>1)))
result[i]=0;//0表示不行
else{
result[i]=1;
for(j=0,m=0;j<len_input;j++){//开始一个个地匹配,j定位标准数组,m定位输入数组bug2!!!!
if(str[j]=='*'){
if((len_str-len_input)==1){//空串
fail=0;//标准数组移动,输入数组不移动
continue;
}
else if(len_input>len_str){//代表一个或者多个bug3!!!!
fail=1;
for(k=0;k<(len_input-len_str+1);k++){//差为0配一个,差为1配两个
fail=1;//每一次匹配前都要置位
for(p=0;p<len_bad;p++){
if(input[i][k+m]==bad[p]){
fail=0;
break;
}
}
if(fail==1)//匹配失败
break;
}
m=m+len_input-len_str+1;//移动输入数组指针
}
else{
fail=1;
}
}
else if(str[j]=='?'){
fail=1;
for(k=0;k<len_good;k++){
if(input[i][m]==good[k]){
fail=0;
break;
}
}
m++;
}
else if(input[i][m]==str[j]){
fail=0;
m++;
continue;
}
else{
fail=1;
}
if(fail){//有一个不匹配这个串就废了
result[i]=0;
break;
}
}
}
}
for(i=0;i<num;i++){
if(result[i]==0){
printf("NO\n");
}
else{
printf("YES\n");
}
}
return 0;
}
这段代码bug重重,但是已经可以体现题主的思路了,算法还是用心地想了一阵子的,所以觉得问题不大——然鹅,接下来题主垃圾的代码水平与菜菜的代码风格欺骗了自己..........
debug!!!!
这里,我在上述代码段已经将出现bug的地方标注出来了,相信聪明一点的宝宝们很快就可以看出来,然鹅题主de了3个多小时..........
错误点1——限制条件不够:
错解:
void converse(char str[]){
int len,i;
len=strlen(str);
for(i=0;i<len;i++){
if(('a'-str[i])>0){//bug1!!!!
str[i]=str[i]+32;//全部变成小写的
}
正解:
void converse(char str[]){
int len,i;
len=strlen(str);
for(i=0;i<len;i++){
if((('Z'-str[i])>=0)&&((str[i]-'A')>=0)){//这里出了一个bug,限制条件不够!!!导致会有其他情况出现!
str[i]=str[i]+32;//全部变成小写的
}
}
}
在没有改出这个bug之前,任何输入得到的结果都是NO,存入数组中的元素是一串串的\0,很无奈,后来发现,gets会将回车换成\0储存,‘a’-str[i]>0这个条件无疑太弱了,会改变很多不需要改变的字符,本意仅仅是改变大写字母呀~~!然后就增加了限制条件!!
debug小技巧:
先注释掉converse函数,很多时候,代码需要分别分段测试的!!!(这就是有子函数的好处!!!!)
错误点2和3——变量名错误与不等号的边界条件(等号)缺失:
错解:
for(j=0,m=0;j<len_input;j++){//开始一个个地匹配,j定位标准数组,m定位输入数组bug2!!!!
if(str[j]=='*'){
if((len_str-len_input)==1){//空串
fail=0;//标准数组移动,输入数组不移动
continue;
}
else if(len_input>len_str){//代表一个或者多个bug3!!!!
fail=1;
for(k=0;k<(len_input-len_str+1);k++){//差为0配一个,差为1配两个
fail=1;//每一次匹配前都要置位
for(p=0;p<len_bad;p++){
if(input[i][k+m]==bad[p]){
正解:
for(j=0,m=0;j<len_str;j++){//开始一个个地匹配,j定位标准数组,m定位输入数组
if(str[j]=='*'){
if((len_str-len_input)==1){//空串
fail=0;//标准数组移动,输入数组不移动
continue;
}
else if(len_input>=len_str){//代表一个或者多个
fail=1;
for(k=0;k<(len_input-len_str+1);k++){//差为0配一个,差为1配两个
fail=1;//每一次匹配前都要置位
debug方法:
1,自己构建了测试集(这一点很重要)!!!
2,肉眼查看(这一点据说需要积累)
3,断点调试(常规方法)
de完这些bug后,发现已经没有逻辑语法错误了,但是提交上去,time超时了,当时内心简直绝望..........
(为什么de了那么久的bug,评测机却如此地对待我.......)
冷静,冷静,最后比对了一下,想了想自己的代码还有哪些地方可以优化,
这个时候,就需要参考参考网上别人的代码了......
果然发现了一个自己智障的地方........(跟别人不一样)
char input[100][100];
二维数组随便开????自己其实每次用的都是一维数组呀,只要一维数组就够用了呢~~~!!!
然后改成了一维数组,就ac了!!!!
开心o(* ̄▽ ̄*)ブ
但是不明白为什么明明应该是空间复杂度不对的问题评测机却说成是时间复杂度的问题????
为什么呐???希望有小伙伴可以看出来,然后(在评论区)指出一下~~~~
错误点4:
不要随随便便开高维数组,空间能节省就尽量节省!!!!!
写完算法记得优化~~~!!!
最后,自己还发现了一个可以优化的地方,就是循环太多了,检测是不是good或着bad数组就用了一个循环,完全没必要的
于是就把原来的注释掉了,改成了现在的写法
fail=1;
for(k=0;k<(len_input-len_str+1);k++){//差为0配一个,差为1配两个
fail=1;//每一次匹配前都要置位
/*for(p=0;p<len_bad;p++){
if(input[k+m]==bad[p]){
fail=0;
break;
}
}*/
if(no[input[k+m]-'a']==1){
fail=0;
成功地使时间复杂度小了很多呢~~~!!!
最终版本:
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
void converse(char str[]){
int len,i;
len=strlen(str);
for(i=0;i<len;i++){
if((('Z'-str[i])>=0)&&((str[i]-'A')>=0)){//这里出了一个bug,限制条件不够!!!导致会有其他情况出现!
str[i]=str[i]+32;//全部变成小写的
}
}
}
int main()
{
int i,j,k,p,m,num,spot=-1,len_good,len_bad,len_input,len_str,fail;
int yes[26]={0};
int no[26]={0};
char good[50];
char bad[50];
char str[100];//记得改!!!
char input[100];
int result[100];
gets(good);
converse(good);
gets(str);
converse(str);
scanf("%d",&num);
len_good=strlen(good);
len_bad=26-strlen(good);
for(i=0;i<len_good;i++){//看看good数组里面有哪些字母
yes[(good[i]-'a')]=1;
}
for(i=0,j=0;i<26;i++){//找全bad数组
if(yes[i]==0){
bad[j]=(char)('a'+i);
no[i]=1;
j++;
}
}
len_str=strlen(str);
for(i=0;i<len_str;i++){
if(str[i]=='*')
spot=i;
}
getchar();
for(i=0;i<num;i++){//根据多少行来计算的
gets(input);
converse(input);
len_input=strlen(input);
if(((spot<0)&&(len_input!=len_str))||((spot>=0)&&((len_str-len_input)>1)))
result[i]=0;//0表示不行
else{
result[i]=1;
for(j=0,m=0;j<len_str;j++){//开始一个个地匹配,j定位标准数组,m定位输入数组
if(str[j]=='*'){
if((len_str-len_input)==1){//空串
fail=0;//标准数组移动,输入数组不移动
continue;
}
else if(len_input>=len_str){//代表一个或者多个
fail=1;
for(k=0;k<(len_input-len_str+1);k++){//差为0配一个,差为1配两个
fail=1;//每一次匹配前都要置位
/*for(p=0;p<len_bad;p++){
if(input[k+m]==bad[p]){
fail=0;
break;
}
}*/
if(no[input[k+m]-'a']==1){
fail=0;
}
if(fail==1)//匹配失败
break;
}
m=m+len_input-len_str+1;//移动输入数组指针
}
else{
fail=1;
}
}
else if(str[j]=='?'){
fail=1;
/*for(k=0;k<len_good;k++){
if(input[m]==good[k]){
fail=0;
break;
}
}*/
if(yes[input[m]-'a']==1){
fail=0;
}
m++;
}
else if(input[m]==str[j]){
fail=0;
m++;
continue;
}
else{
fail=1;
}
if(fail){//有一个不匹配这个串就废了
result[i]=0;
break;
}
}
}
}
for(i=0;i<num;i++){
if(result[i]==0){
printf("NO\n");
}
else{
printf("YES\n");
}
}
return 0;
}
注释写的有点乱,但是大体上逻辑还是非常清晰的,希望大家不吝赐教~~~!