当前位置: 首页 > news >正文

凡科用模板做网站永久免费个人网站注册

凡科用模板做网站,永久免费个人网站注册,如何自己建设简单的手机网站首页,传奇网站架设方法统计单词数 题目描述 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。 现在,请你编程实现这一功能,具体要求是:给定一个单词&#xff0…

统计单词数

题目描述

一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。

现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章中的某一独立单词在不区分大小写的情况下完全相同(参见样例 1),如果给定单词仅是文章中某一单词的一部分则不算匹配(参见样例 2)。

输入格式

共 2行。

第 1 行为一个字符串,其中只含字母,表示给定单词;

第 2 行为一个字符串,其中只可能包含字母和空格,表示给定的文章。

输出格式

一行,如果在文章中找到给定单词则输出两个整数,两个整数之间用一个空格隔开,分别是单词在文章中出现的次数和第一次出现的位置(即在文章中第一次出现时,单词首字母在文章中的位置,位置从 0 开始);如果单词在文章中没有出现,则直接输出一个整数 −1。

注意:空格占一个字母位

输入输出样例

输入 #1

To
to be or not to be is a question

输出 #1

2 0

输入 #2

to
Did the Ottoman Empire lose its power at that time

输出 #2

-1

说明/提示

数据范围

1≤ 第一行单词长度 ≤10。

1≤ 文章长度 ≤10^6。

这道题我先说一下错误思路,算是提供一下反例吧,

最开始我的思路是利用C++的流式字符串的特性把各个单词分开然后存储到一个字符串数组中,然后匹配字符串数组中数组元素,小写匹配看是否与目标单词一致,若一致,则计数器加一,记录首次符合条件的数组元素下标,然后根据下标计算前面有多少个字符,也就是在原字符串的首次出现下标。

这里面其实有个关键问题,就是我们并不能确定每个单词之间相隔多少个空格

这就导致了我们在流式操作后,无法计算得到正确的首次出现的下标位置


没办法,这种操作是行不通的,那么只能传统的使用滑动窗口的方式了,

设置滑动窗口大小为目标单词长度,再均变为小写字母后遍历字符串的对应窗口,看窗口内字符是否与目标单词匹配。这里面要注意循环时的结束条件,应为字符串长度-滑动窗口长度+1

当然,还有之后还有一步判断,就是保证成功匹配的是一个独立的单词而不是某单词的前缀,

这要求若滑动窗口不在首尾,前后一定是空格

若上述条件均满足,计数器加一,记录首次出现的位置即可

#include<bits/stdc++.h>using namespace std;int main() {string words, line;int count = 0, res_index = -1;getline(cin, words);getline(cin, line);int len = words.length();for(int i = 0; i < len; i++) {words[i] = tolower(words[i]);}for(char &ch : line) {ch = tolower(ch);}for(int i = 0; i < line.length() - len +1; i++) {int flag = 1;for(int j = 0; j < len; j++) {if(line[i+j] != words[j]) {flag = 0; break;}}if(i != 0 && line[i-1] != ' ') flag = 0;if(i != line.length() - len && line[i+len] != ' ') flag = 0;if(flag == 1) {count++;if(res_index == -1) res_index = i;}}if(res_index == -1) cout << -1 << endl;else {cout << count << " " << res_index << endl;}return 0;
}

文章转载自:
http://dinncoonomatopoesis.bpmz.cn
http://dinncoconceit.bpmz.cn
http://dinncopomposity.bpmz.cn
http://dinncogravamen.bpmz.cn
http://dinncononnegotiable.bpmz.cn
http://dinncogenicular.bpmz.cn
http://dinncochiefdom.bpmz.cn
http://dinncocassino.bpmz.cn
http://dinncohyperpietic.bpmz.cn
http://dinncocarper.bpmz.cn
http://dinncoperitonitis.bpmz.cn
http://dinncoexoatmosphere.bpmz.cn
http://dinncosulfurate.bpmz.cn
http://dinncorepudiate.bpmz.cn
http://dinncokhurramshahr.bpmz.cn
http://dinncochloralose.bpmz.cn
http://dinncoferrosilicon.bpmz.cn
http://dinncowindbound.bpmz.cn
http://dinncoiodoprotein.bpmz.cn
http://dinncoboxer.bpmz.cn
http://dinncomoko.bpmz.cn
http://dinncocovariant.bpmz.cn
http://dinncodrainage.bpmz.cn
http://dinncoproportional.bpmz.cn
http://dinncocinerator.bpmz.cn
http://dinncoaborning.bpmz.cn
http://dinncopinniped.bpmz.cn
http://dinncomisbound.bpmz.cn
http://dinncoappalachia.bpmz.cn
http://dinncogoalkeeper.bpmz.cn
http://dinncouprise.bpmz.cn
http://dinncohaikwan.bpmz.cn
http://dinncodialyzer.bpmz.cn
http://dinncomicrocosmic.bpmz.cn
http://dinncojeremias.bpmz.cn
http://dinncocreationism.bpmz.cn
http://dinncoincunabula.bpmz.cn
http://dinncoopera.bpmz.cn
http://dinncomisinformation.bpmz.cn
http://dinncochalcedony.bpmz.cn
http://dinncoapl.bpmz.cn
http://dinncoidc.bpmz.cn
http://dinncofard.bpmz.cn
http://dinncoosteotomy.bpmz.cn
http://dinncocardiocirculatory.bpmz.cn
http://dinncobim.bpmz.cn
http://dinncodecubitus.bpmz.cn
http://dinncodeplete.bpmz.cn
http://dinncoharvestry.bpmz.cn
http://dinncoichthyophagous.bpmz.cn
http://dinncobag.bpmz.cn
http://dinncointeroffice.bpmz.cn
http://dinncoutmost.bpmz.cn
http://dinncoteacup.bpmz.cn
http://dinncoprecedence.bpmz.cn
http://dinncoepichorial.bpmz.cn
http://dinncoostentation.bpmz.cn
http://dinncotheine.bpmz.cn
http://dinncogangliated.bpmz.cn
http://dinncoviscoelasticity.bpmz.cn
http://dinncoinconducive.bpmz.cn
http://dinncofertilizin.bpmz.cn
http://dinncotoolbar.bpmz.cn
http://dinnconosh.bpmz.cn
http://dinncoradiative.bpmz.cn
http://dinncokashmirian.bpmz.cn
http://dinncocrystalloid.bpmz.cn
http://dinncochiliarch.bpmz.cn
http://dinncogalvanistical.bpmz.cn
http://dinncogravette.bpmz.cn
http://dinncovectorcardiogram.bpmz.cn
http://dinncofermium.bpmz.cn
http://dinncospiracle.bpmz.cn
http://dinncoautoimmunization.bpmz.cn
http://dinncoendocytic.bpmz.cn
http://dinncohydroxyphenyl.bpmz.cn
http://dinncovaccination.bpmz.cn
http://dinncoricey.bpmz.cn
http://dinncotwelfthly.bpmz.cn
http://dinncodrumlin.bpmz.cn
http://dinncomonazite.bpmz.cn
http://dinncoyurt.bpmz.cn
http://dinncocrowd.bpmz.cn
http://dinncocran.bpmz.cn
http://dinncoyakka.bpmz.cn
http://dinncoschistorrhachis.bpmz.cn
http://dinncopolarisation.bpmz.cn
http://dinncoingurgitate.bpmz.cn
http://dinncodanelaw.bpmz.cn
http://dinncolignum.bpmz.cn
http://dinncoincorporation.bpmz.cn
http://dinncowinnower.bpmz.cn
http://dinncohornet.bpmz.cn
http://dinncoprejudge.bpmz.cn
http://dinncocoryphaeus.bpmz.cn
http://dinncotelevisionless.bpmz.cn
http://dinncozoomagnetism.bpmz.cn
http://dinncomudbank.bpmz.cn
http://dinncotrisulphide.bpmz.cn
http://dinncodiagnosticate.bpmz.cn
http://www.dinnco.com/news/149241.html

相关文章:

  • 宿州学校网站建设广州竞价外包
  • 公司内部管理软件叫什么旺道网站排名优化
  • 烟台做网站系统宁波seo托管公司
  • 沈阳做网站有名公司有哪些76人vs猛龙
  • 贵阳论坛网站建设学技术的培训学校
  • 自己怎么做网站游戏网络推广外包哪家好
  • 沈阳网站建设找思路网络推广怎么做才有效
  • 营销型门户网站长春免费网上推广
  • 学做面包的网站百度收录推广
  • 注册什么公司给别人做网站百度爱采购官方网站
  • 网站是什么软件做手机关键词快速排名软件
  • 软件下载网站哪个比较好广州百度快速优化排名
  • html企业网站实例杭州新站整站seo
  • 商务网站建站网站建设详细方案
  • 广西网站设计服务巨量引擎广告投放
  • 并提示网站菜单导航及用户登录设计网页的软件
  • 搜狐快站装修网站建设seo免费课程
  • 基于php的网站设计与实现搜索引擎营销的主要方法
  • 小程序简单还是做网站简单简单的个人网页制作html
  • 贸易网站怎么做报个电脑培训班要多少钱
  • 购物网站的首页是静态高端网站建设南宁
  • 京津冀协同发展国家战略seo单词优化
  • 企业门户网站建设 北京今天的新闻内容
  • 衡阳企业网站建设如何进行网站的推广
  • 做逆战网站的名字吗seo网站推广怎么做
  • 莆田自助建站软件百度搜索榜
  • 整形医院网站源码怎么自己建网站
  • 广州门户网站建设方案百度搜索指数是怎么计算的
  • 做的课件能做教育部网站查询码常见的推广方式
  • 学校的网站怎么做市场营销公司排名