当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
- 方程推导:二阶有源带通滤波器设计!(下载:教程+原理图+视频+代码)
- What is the ranking of Guosen Securities? Is it safe to open a stock account?
- Datasets dataset class (2)
- Usage of unique function
- Why is there always a space (63 or 2048 sectors) in front of the first partition when partitioning a disk
- 使用宝塔面板部署flask环境
- 【async/await】--异步编程最终解决方案
- fileinput.js php,fileinput
- (improved) bubble sorting and (improved) cocktail sorting
- Experience sharing of mathematical modeling: comparison between China and USA / reference for topic selection / common skills
猜你喜欢

Sword finger offer 45.61 Sort (simple)

Usage of unique function

Electron

Sword finger offer 21.57.58 I Double pointer (simple)

Codeforces Global Round 21A~D

Correlation of XOR / and

Leaflet load day map

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册

How to call self written functions in MATLAB

ArcGIS batch export layer script
随机推荐
GDAL grid data types and their type codes
年薪50万是一条线,年薪100万又是一条线…...
Hard (magnetic) disk (I)
C | analysis of malloc implementation
fileinput. js php,fileinput
启动Redis报错:Could not create Server TCP listening socket *:6379: bind: Address already in use–解决办法
Sword finger offer 15.65.56 I 56Ⅱ. Bit operation (simple - medium)
Oracle ASMM和AMM
备战数学建模32-相关性分析2
Equation derivation: second order active bandpass filter design! (download: Tutorial + schematic + Video + code)
Complimentary Book Cognitive Control: how does our brain accomplish tasks?
Relevant knowledge of information entropy
Flex & bison start
ArcGIS batch export layer script
C语言基础知识入门(大全)「建议收藏」
MySQL master-slave replication and read-write separation
Never use redis expired monitoring to implement scheduled tasks!
GDAL multiband synthesis tool
从Celsius到三箭:加密百亿巨头们的多米诺,史诗级流动性的枯竭
Freefilesync folder comparison and synchronization software