当前位置:网站首页>信息学奥赛一本通 1400:统计单词数 (字符串匹配)
信息学奥赛一本通 1400:统计单词数 (字符串匹配)
2022-06-26 13:56:00 【GHOSTANDBREAD】
信息学奥赛一本通(C++版)在线评测系统 (ssoier.cn)
【题目描述】
一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。
现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章中的某一独立单词在不区分大小写的情况下完全相同(参见样例1),如果给定单词仅是文章中某一单词的一部分则不算匹配(参见样例2)。
【输入】
第 1 行为一个字符串,其中只含字母,表示给定单词;
第 2 行为一个字符串,其中只可能包含字母和空格,表示给定的文章。
【输出】
只有一行,如果在文章中找到给定单词则输出两个整数,两个整数之间用一个空格隔开,分别是单词在文章中出现的次数和第一次出现的位置(即在文章中第一次出现时,单词首字母在文章中的位置,位置从0开始);如果单词在文章中没有出现,则直接输出一个整数-1。
【输入样例】
To
to be or not to be is a question【输出样例】
2 0【提示】
样例输入:
样例 #2:
to Did the Ottoman Empire lose its power at that time
样例输出:
样例 #2:
-1思路:
主串从头遍历,当遇到有字母和匹配串第一个字母相同时(不区分大小写),开始比较这个字母后面的字母是否和匹配串一样,不一样就结束比较,都一样时,判断这个字符串在主串中的位置是否前后都有空格,注意位置在主串第一个和最后一个时的边界问题
代码:
#include<iostream>
#include<cstring>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
int res;
int minn = inf;
int main() {
string s, a;
cin >> s;
getchar();
getline(cin, a);
for(int i = 0; i < a.size(); i ++) {
int j = 0;
if(a[i] == s[0] || a[i] == (s[0] - 32) || a[i] == (s[0] + 32)) {
for(j = 0; j < s.size(); j ++) {
if(a[i + j] == (s[j] + 32) || a[i + j] == (s[j] - 32) || a[i + j] == s[j]) {
continue;
} else break;
}
if(j == s.size() && (a[i - 1] == ' ' || i == 0) && (a[i + j] == ' ' || (i + j - 1) == (a.size() - 1))) {
res ++;
minn = min(minn, i);
}
}
}
if(res)
cout << res << " " << minn;
else
cout << -1;
return 0;
}
边栏推荐
- 这才是优美的文件系统挂载方式,亲测有效
- Eigen(3):error: ‘Eigen’ has not been declared
- 9 articles, 6 interdits! Le Ministère de l'éducation et le Ministère de la gestion des urgences publient et publient conjointement neuf règlements sur la gestion de la sécurité incendie dans les établ
- wptx64能卸载吗_win10自带的软件哪些可以卸载
- Leaflet load day map
- 扩展-Hooks
- VIM auto fill auto indent explanation
- Sword finger offer 45.61 Sort (simple)
- Eigen(3):error: ‘Eigen’ has not been declared
- Oracle ASMM和AMM
猜你喜欢

How to mount cloud disks in ECS

'coach, I want to play basketball!'—— AI Learning Series booklet for system students

Eigen(3):error: ‘Eigen’ has not been declared

Jianzhi offer 43.47.46.48 dynamic planning (medium)

Sword finger offer 06.24.35 Linked list

MySQL master-slave replication and read-write separation

Chinese output of PostGIS console is garbled

常用控件及自定义控件

How to personalize VIM editor format (DIY)

Hard (magnetic) disk (II)
随机推荐
Is the account opening link given by the broker manager safe? Who can I open an account with?
【soloπ】adb连接单个多个手机
How to convert data in cell cell into data in matrix
Sectigo的IP证书申请流程
Leaflet load day map
Flex & Bison 开始
Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills
Equation derivation: second order active bandpass filter design! (download: Tutorial + schematic + Video + code)
Sword finger offer 15.65.56 I 56Ⅱ. Bit operation (simple - medium)
通俗语言说BM3D
Extended hooks
C language | Consortium
vmware部分设置
Hard (magnetic) disk (I)
Relevant knowledge of information entropy
Transformers datacollatorwithpadding class
Freefilesync folder comparison and synchronization software
Related knowledge of libsvm support vector machine
登录认证服务
ArcGIS secondary development - arcpy delete layer