C++ 算法提高 字符串匹配
#include<iostream>
//#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
int tig=0,n,l=1;
cin>>s>>tig>>n;
if(n==0)
return 0;
int a;
string st[n];
for(int i=0;i<n;i++)
{
cin>>st[i];
}
for(int i=0;i<n;i++)
{
for(int k=0;k<st[i].length()-s.length()+1;k++)
{
int l=1;
if(st[i][k]==s[0]||st[i][k]+32==s[0]||st[i][k]-32==s[0])
{
l=2;
int d=0;
if(tig==1)
{
for(int j=k;j<s.length()+k;j++,d++)
{
if(st[i][j]!=s[d])
{
l=0;
break;
}
}
}
else
{
for(int j=k;j<s.length()+k;j++,d++)
{
if(st[i][j]!=s[d]&&st[i][j]+32!=s[d]&&st[i][j]-32!=s[d])
{
l=0;
break;
}
}
}
}
if(l==2)
{
cout<<st[i]<<endl;
break;
}
}
}
return 0;
}
其中需要注意的是1个验证
1.包含重复的数据
如
p
1
1
pop
它会输出两次pop.
用到暴力查找和字符串数组