字符串匹配【第二次CCF计算机软件能力认证】

字符串匹配

给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行。

你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符;当选项关闭时,表示同一个字母的大写和小写看作相同的字符。

输入格式
输入的第一行包含一个字符串 S,由大小写英文字母组成。

第二行包含一个数字,表示大小写敏感的选项,当数字为 0 时表示大小写不敏感,当数字为 1 时表示大小写敏感。

第三行包含一个整数 n,表示给出的文字的行数。

接下来 n 行,每行包含一个字符串,字符串由大小写英文字母组成,不含空格和其他字符。

输出格式
输出多行,每行包含一个字符串,按出现的顺序依次给出那些包含了字符串 S 的行。

数据范围
1≤n≤100,
每个字符串的长度不超过 100。

输入样例:
Hello
1
5
HelloWorld
HiHiHelloHiHi
GrepIsAGreatTool
HELLO
HELLOisNOTHello
输出样例:
HelloWorld
HiHiHelloHiHi
HELLOisNOTHello
样例解释
在上面的样例中,第四个字符串虽然也是 Hello,但是大小写不正确。

如果将输入的第二行改为 0,则第四个字符串应该输出。

代码1

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<cctype>
using namespace std;

#define X first
#define Y second

typedef pair<int,int> pii;
typedef long long LL;
const char nl = '\n';
const int N = 110;
const int M = 2e5+10;
int n,m;
char p[N];
int ne[N];

void solve(){
	cin >> p + 1;	//c数组可以从下标1开始读入,string不可以
	int len_p = strlen(p+1);	//获取长度,记得是s+1而不是s
	int op,m;
	cin >> op >> m;
	if(!op){
		for(int i = 2,j = 0; i <= len_p; i ++ ){	//从第二位开始比较构造(同一字符串同一位置肯定相同)
			while(j && tolower(p[i]) != tolower(p[j + 1]))j = ne[j];
			if(tolower(p[i]) == tolower(p[j + 1]))j ++;
			ne[i] = j;
		}	//字串
		while(m -- ){
			char s[110] = {0};
			//char p[110] = {};
			//memset(p,0,sizeof p);
			//char p[110] = {'\0'};
			cin >> s + 1;
			int len_s = strlen(s+1);
			for(int i = 1,j = 0; i <= len_s; i ++ ){
				while(j && tolower(s[i]) != tolower(p[j + 1]))j = ne[j];
				if(tolower(s[i]) == tolower(p[j + 1]))j ++;
				if(j == len_p){	//匹配成功
					cout << s + 1 << nl;
					break;
				}
			}
		}
	}
	else{
		for(int i = 2,j = 0; i <= len_p; i ++ ){	//从第二位开始比较构造(同一字符串同一位置肯定相同)
			while(j && p[i] != p[j + 1])j = ne[j];
			if(p[i] == p[j + 1])j ++;
			ne[i] = j;
		}	//字串
		while(m -- ){
			char s[110] = {0};
			//char p[110] = {};
			//memset(p,0,sizeof p);
			//char p[110] = {'\0'};
			cin >> s + 1;
			int len_s = strlen(s+1);
			for(int i = 1,j = 0; i <= len_s; i ++ ){
				while(j && s[i] != p[j + 1])j = ne[j];
				if(s[i] == p[j + 1])j ++;
				if(j == len_p){	//匹配成功
					cout << s + 1 << nl;
					break;
				}
			}
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	solve();
}

小结

  1. c数组可以从1开始读入(如cin >> s + 1)然而string不支持,kmp还是用c数组较好
  2. 在循环中c数组初始化可以(char s[110] = {}😉
  3. 注意kmp的一些细节
  4. 取c数组长度可以用strlen函数

string.find()

作用

string的find()函数用于找出子串在母串中的位置。

定义

find(str,position)

  • str:是要找的元素
  • position:字符串中的某个位置,表示从从这个位置开始的字符串中找指定元素(不填第二个参数,默认从字符串的开头进行查找)
返回值
  1. 如果子串在母串中存在,返回值为最近一个匹配字串首字母下标(c++下标默认从0开始)
    注意即使position的位置不是0,返回的下标也是相对母串而言得的,而不是相对于position而言的
  2. 如果不存在,则返回string::npos,在C++中常量npos是这样定义的:
    static const size_t npos = -1;
    即常量npos定义的值为-1. 但又因为npos 的类型size_t是无符号整数类型,所以npos实际上是一个正数,并且是size_t类型的最大值18446744073709551615。
    find函数返回的值赋给size_t类型的变量position,而size_t类型的变量position是永远大于等于0,所以即使find返回npos,if条件也为true。
延伸用法
  1. s.find_first_of() //返回第一次出现的位置(也相当于s.find()的默认返回最近一个匹配字串首字母下标)
  2. s.find_last_of() //返回最后一次出现的位置(注意这里的返回值是最后一个匹配字串尾字母下标)
  3. s.rfind() //逆向寻找(注意子串没有逆向:相当于s.find_last_of()返回最后一个匹配字串首字母下标
    正向查找与反向查找得到的位置不相同说明子串不唯一
  4. 查找所有子串在母串中出现的位置
    while((position=dad.find(son,position))!=-1)
    {
        cout<<"position  "<<i<<" : "<<position<<endl;
        position++;
        i++;
    }

transform

头文件algorithm
定义

transform(处理对象容器起始地址,处理对象容器结束地址,存放结果的容器地址,处理操作(可自定义))

一般用法

transform( str.begin() , str.end() , str.begin() , ::toupper );//化为大写
transform( str.begin() , str.end() , str.begin() , ::tolower );//化为小写

注意
  1. 存放结果的string应该要和transform前的str长度一致或者更长,否则会被截断
  2. 如::toupper只将小写字母变为大写字母,其他字符忽略

代码2

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;

#define X first
#define Y second

typedef pair<int,int> pii;
typedef long long LL;
const char nl = '\n';
const int N = 1e6+10;
const int M = 2e5+10;
int n,m;

void solve(){
	string son;
	cin >> son;
	int op,m;
	cin >> op >> m;
	if(op){
		while(m -- ){
			string dad;
			cin >> dad;
			if(dad.find(son) != -1)cout << dad << nl;
		}
	}
	else{
		transform(son.begin(),son.end(),son.begin(),::toupper);
		while(m -- ){
			string dad;
			cin >> dad;
			string t = dad;
			transform(dad.begin(),dad.end(),dad.begin(),::toupper);
			if(dad.find(son) != -1)cout << t << nl;
		}
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	solve();
}
posted @ 2023-03-06 13:16  Keith-  阅读(20)  评论(0编辑  收藏  举报