当前位置:网站首页>信息学奥赛一本通 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;
}
边栏推荐
猜你喜欢

Naacl2022: (code practice) good visual guidance promotes better feature extraction, multimodal named entity recognition (with source code download)

Knowledge about the determination coefficient R2 and the relationship with the correlation coefficient

Win10 home vs pro vs enterprise vs enterprise LTSC

The annual salary of 500000 is one line, and the annual salary of 1million is another line

Electron

Electron

聊聊 RPA 方向的规划:简单有价值的事情长期坚持做

布局管理器~登录界面的搭建实例

Gartner 2022 Top Strategic Technology Trends Report

Deploy the flask environment using the pagoda panel
随机推荐
GDAL grid data types and their type codes
New specification of risc-v chip architecture
这才是优美的文件系统挂载方式,亲测有效
Gartner 2022 Top Strategic Technology Trends Report
Transformers datacollatorwithpadding class
Eigen(3):error: ‘Eigen’ has not been declared
Jianzhi offer 43.47.46.48 dynamic planning (medium)
【soloπ】adb连接单个多个手机
聊聊 RPA 方向的规划:简单有价值的事情长期坚持做
C | analysis of malloc implementation
登录认证服务
常用控件及自定义控件
Usage of unique function
Electron
ArcGIS cannot be opened and displays' because afcore cannot be found ' DLL, solution to 'unable to execute code'
券商经理给的开户链接安全吗?找谁可以开户啊?
Atcoder 238
数学建模经验分享:国赛美赛对比/选题参考/常用技巧
Freefilesync folder comparison and synchronization software
MHA high availability coordination and failover