题解 | #字符串通配符#
字符串通配符
http://www.nowcoder.com/practice/43072d50a6eb44d2a6c816a283b02036
#include<iostream>
using namespace std;
int main()
{
string a,b;
while(getline(cin,a),getline(cin,b))
{
int c=a.length(),d=b.length();
for(int i=0;i<c;i++)
{
if(a[i]>='A'&&a[i]<='Z')
{
a[i]=a[i]-'A'+'a';
}
}
for(int i=0;i<d;i++)
{
if(b[i]>='A'&&b[i]<='Z')
{
b[i]=b[i]-'A'+'a';
}
}
for(int i=0;i<c;i++)
{
if(a[i]=='*'||a[i]=='?')
{
break;
}
else if(i==c-1)
{
if(a.length()!=b.length())
{
goto x;
}
}
}
for(int i=0;i<c;i++)
{
if(a[i]!='*'&&a[i]!='?')
{
for(int j=i;j<c;j++)
{
if(a[j]=='*'||a[j]=='?')
{
string c=a.substr(i,j-i);
if(b.find(c)<b.length())
{
b.erase(b.find(c),j-i);
i=j;
break;
}
else
{
goto x;
}
}
else if(j==c-1)
{
string c=a.substr(i,j-i+1);
if(b.rfind(c)<b.length())
{
if(b.length()-b.rfind(c)>c.length())
{
goto x;
}
b.erase(b.find(c),j-i+1);
if(b.length()==0)
{
goto y;
}
i=j;
break;
}
else
{
goto x;
}
}
}
}
}
if(b.length()==0)
{
goto y;
}
for(int i=0;i<b.length();i++)
{
if((b[i]>='a'&&b[i]<='z')||(b[i]>='0'&&b[i]<='9'))
{
continue;
}
else
{
goto x;
}
}
y:
cout<<"true";
break;
x:
cout<<"false";
}
return(0);
}
using namespace std;
int main()
{
string a,b;
while(getline(cin,a),getline(cin,b))
{
int c=a.length(),d=b.length();
for(int i=0;i<c;i++)
{
if(a[i]>='A'&&a[i]<='Z')
{
a[i]=a[i]-'A'+'a';
}
}
for(int i=0;i<d;i++)
{
if(b[i]>='A'&&b[i]<='Z')
{
b[i]=b[i]-'A'+'a';
}
}
for(int i=0;i<c;i++)
{
if(a[i]=='*'||a[i]=='?')
{
break;
}
else if(i==c-1)
{
if(a.length()!=b.length())
{
goto x;
}
}
}
for(int i=0;i<c;i++)
{
if(a[i]!='*'&&a[i]!='?')
{
for(int j=i;j<c;j++)
{
if(a[j]=='*'||a[j]=='?')
{
string c=a.substr(i,j-i);
if(b.find(c)<b.length())
{
b.erase(b.find(c),j-i);
i=j;
break;
}
else
{
goto x;
}
}
else if(j==c-1)
{
string c=a.substr(i,j-i+1);
if(b.rfind(c)<b.length())
{
if(b.length()-b.rfind(c)>c.length())
{
goto x;
}
b.erase(b.find(c),j-i+1);
if(b.length()==0)
{
goto y;
}
i=j;
break;
}
else
{
goto x;
}
}
}
}
}
if(b.length()==0)
{
goto y;
}
for(int i=0;i<b.length();i++)
{
if((b[i]>='a'&&b[i]<='z')||(b[i]>='0'&&b[i]<='9'))
{
continue;
}
else
{
goto x;
}
}
y:
cout<<"true";
break;
x:
cout<<"false";
}
return(0);
}