复习3种语言的字符串查找的函数(方法)
1. 题目
礼物
★问题描述
又是一年情人节。奕哥要给女朋友送礼物。奕哥的女朋友喜欢一个字符串S(只包含小写字母)。奕哥已经选好了几个礼物,每个礼物都有一个名字(只包含小写字母)。如果礼物的名字是S的子串。则奕哥的女朋友会喜欢这个礼物。奕哥不想花冤枉钱。所以想要提前知道女朋友喜不喜欢?但是字符串太长了,奕哥自己数不过来,你能帮帮他吗?
★数据输入
输入第一行包括一个字符串S(1<=|S|<=100000),表示奕哥女朋友喜欢的字符串。
第二行包括一个整数N(1<=N<=10),表示奕哥已经选好的礼物。
接下来N行,每行一个字符串Ti(1<=|Ti|<=|S|)表示奕哥选好的第i个礼物的名字。
★数据输出
输出N行。如果喜欢第i个礼物,第i行输出"YES",否则输出“NO"。(不带引号)
★输入示例1
abcdef
2
bcd
cb
★输出示例1
YES
NO
★输入示例2
aaabaaaa
2
aaaaa
aaa
★输出示例2
NO
YES
2. 字符串查找的函数(方法)复习(C、C++、Python)
C语言——字符串查找的函数strstr(str1,str2)
strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返NULL(NULL是空指针)。它位于头文件 “string.h”中。
代码实现
#include<stdio.h>
#include<string.h>
const int N=100005;
int main()
{
char x[N];
char y[N];
gets(x);
int n;
scanf("%d",&n);//scanf把换行符保留在输入流中,需要用getchar吸收
getchar();
int z[n];
int i;
for(i=0;i<n;i++)
{
gets(y);
if(strstr(x,y)!=NULL)
{
z[i]=1;
}
else
{
z[i]=0;
}
}
for(i=0;i<n;i++)
{
if(z[i]==1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return 0;
}
更多C语言字符串函数的使用请看li27z
tips:
1.C语言的gets()函数不安全,它不会去检查输入字符串是否超过了字符串数组的长度。
2.C语言的字符串一些的函数也不安全,如strcpy(),它不会去检查拷贝入的数组是否越界,后来的标准加入了strncpy(),限定了可拷贝数组的最大长度。
C++的string类的find()方法
C++在C的基础上增加string类来处理字符串,string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos。(返回值可以看成是一个int型的数),具体重载的函数定义如下:
int find(char c, int pos = 0) const;//从pos开始查找字符c在当前字符串的位置。
int find(const char *s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置。
int find(const char *s, int pos, int n) const;//从pos开始查找字符串s中前n个字符在当前串中的位置。
int find(const string &s, int pos = 0) const;//从pos开始查找字符串s在当前串中的位置
//查找成功时返回所在位置,失败返回string::npos的值。
代码实现
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
string x;
string y;
int main()
{
cin>>x;
int n;
(cin>>n).get();//cin>>n把换行符保留在输入流中,需要将cin.get()吸收
bool z[n];
for(int i=0;i<n;i++)
{
string::size_type idx;
cin>>y;
idx = x.find(y);
if(idx != string::npos)
{
z[i]=1;
}
else
{
z[i]=0;
}
}
for(int i=0;i<n;i++)
{
if(z[i]==1)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}
stringfind()函数的具体使用方法具体见王陸
tips:要注意cin>>x、cin.get()、cin.getline()对换行符的的读取情况。
python str.find()方法
find()方法检测字符串中是否包含子字符串str,如果指定beg(开始)和end(结束) 范围,则检查是否包含在指定范围内,如果指定范围内如果包含指定索引值,返回的是索引值在字符串中的起始位置。如果不包含索引值,返回-1。语法如下:
str.find(str, beg=0, end=len(string))
代码实现
x=[]
x = input()
n = int(input())
z = [0]*n
for i in range(n):
y = input()
t = x.find(y)
if t!=-1:
z[i]=1
for i in range(n):
if(z[i]==1):
print("YES")
else:
print("NO")
3. 说明
这些都是旁门左道,对我来说能过就行了,想要更快的字符串查找算法可以参考kmp或者字符串哈希。
传送门:KMP
字符串哈希
-------------------------------------分割线---------------------------------------
4.自嘲
以下是我自嘲内容,真的,我真的傻,明明不是很难的题目,我居然想到用求取最大公共子串的方法(动态规划)来判断是否是原字符串的子串,代码一直invalid memory reference,害,后来想想,那个给定的字符串挺长的,还真不可行,最长公共子串是用来求取长度差不多的数组的最长公共子串。
下面给上求取最大公共子串的代码安慰一下自己。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
typedef long long ll;
int lmax;
int pmax;
const int N=100005;
char x[N];
char y[N];
void longest_common_substring(char x[],char y[]);
void longest_common_substring(char x[],char y[])
{
int n,m;
n = strlen(x);
m = strlen(y);
cout<<"n="<<n<<" m="<<m<<endl;
int c[n+1][m+1];
lmax=0;
pmax=0;
for(int i=0;i<=n;i++)
{
c[i][0]=0;
}
for(int i=0;i<=m;i++)
{
c[0][i]=0;
}
for(int i=1;i<=n;i++)
{
cout<<"i="<<i<<endl;
for(int j=1;j<=m;j++)
{
if(x[i-1]!=y[j-1])//注意x,y下标从0开始
c[i][j]=0;
else
{
c[i][j]=c[i-1][j-1]+1;
if(c[i][j]>lmax)
{
lmax=c[i][j];
cout<<"ilmax="<<lmax<<endl;
pmax=i;
cout<<"ipmax="<<pmax<<endl;
}
}
}
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=m;j++)
{
cout<<c[i][j]<<" ";
}
cout<<endl;
}
}
void print_lcs(char x[],int lmax,int pmax)
{
cout<<"plmax="<<lmax<<endl;
if (lmax==0) return ;
for(int i=pmax-lmax;i<pmax;i++)
cout<<x[i]<<" ";
}
int main()
{
cin.getline(x,20);
cin.getline(y,20);
cout<<x<<endl;
cout<<y<<endl;
longest_common_substring(x,y);
cout<<"mlmax="<<lmax<<endl;
cout<<"mpmax="<<pmax<<endl;
print_lcs(x,lmax,pmax);
return 0;
}
以及失败的代码
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
using namespace std;
int lmax;
const int N=100005;
char x[N];
char y[N];
void longest_common_substring(char y[]);
void longest_common_substring(char y[])//寻找最大公共子串,动态规划
{
int n,m;
n = strlen(x);
m = strlen(y);
int c[n+1][m+1];//记录数组
lmax=0;
for(int i=0;i<=n;i++)
{
c[i][0]=0;
}
for(int i=0;i<=m;i++)
{
c[0][i]=0;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(x[i-1]!=y[j-1])//注意字符串x,y下标从0开始
c[i][j]=0;
else
{
c[i][j]=c[i-1][j-1]+1;
if(c[i][j]>lmax)
{
lmax=c[i][j];
if (lmax==strlen(y))//存在就退出这个调用
return ;
}
}
}
}
}
int main()
{
cin.getline(x,N);
int n;
(cin>>n).get();//cin>>n把换行符保留在输入流中,需要将cin.get()吸收
bool z[n];
for(int i=0;i<n;i++)
{
cin.getline(y,N);
longest_common_substring(y);
if(strlen(y)==lmax)
{
z[i]=1;
}
else
{
z[i]=0;
}
}
for(int i=0;i<n;i++)
{
if(z[i]==1)
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
return 0;
}
新手开车,勿喷勿喷