当前位置:网站首页>P1321 单词覆盖还原【入门】
P1321 单词覆盖还原【入门】
2022-07-16 23:59:00 【会编程的露娜】
题目描述
一个长度为 l l l 的字符串中被反复贴有 boy 和 girl 两单词,后贴上的可能覆盖已贴上的单词(没有被覆盖的用句点表示),最终每个单词至少有一个字符没有被覆盖。问贴有几个 boy 几个 girl?
输入格式
一行被被反复贴有 boy 和 girl 两单词的字符串。
输出格式
两行,两个整数。第一行为 boy 的个数,第二行为 girl 的个数。
样例输入
......boyogirlyy......girl.......
样例输出
4
2
提示
数据保证, 3 ≤ l ≤ 255 3\le l\le255 3≤l≤255,字符串仅仅包含如下字符: .bgilory \texttt{.bgilory} .bgilory。
思路;:
题目中说 “每个单词至少有一个没被覆盖” ,那么我们只要在这个单词的长度范围内如果存在字符符合单词原本字符的位置,那么就说明此处是有一个单词的。
#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<queue>
#include<stack>
#include<cmath>
#include<algorithm>
#include<cstring>
using namespace std;
int a[10001]={
0};
int main(){
string str;
int boy=0,girl=0,k=0;
cin>>str;
k=str.length();
for(int i=0;i<k;++i){
if(str[i]=='b'||str[i+1]=='o'||str[i+2]=='y')
++boy;
if(str[i]=='g'||str[i+1]=='i'||str[i+2]=='r'||str[i+3]=='l')
++girl;
}
cout<<boy<<endl<<girl;
return 0;
}
还有其他dalao的思路:
也是很容易理解的:
如果字符是 b 或者 g 开头,那肯定这里就有一个对应的单词,
如果是中间或后面的字符,那么需要它前面的字符不应该和原本单词的字符相同,这样才是一个本覆盖的单词。
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int n=s.size(),b=0,g=0;
//b,g分别为boy的个数和girl的个数
for (int i=0;i<n;i++)
{
if (s[i]=='b') b++;
if (s[i]=='o'&&s[i-1]!='b') b++;
if (s[i]=='y'&&s[i-1]!='o'&&s[i-2]!='b') b++;
//boy的运算
if (s[i]=='g') g++;
if (s[i]=='i'&&s[i-1]!='g') g++;
if (s[i]=='r'&&s[i-1]!='i'&&s[i-2]!='g') g++;
if (s[i]=='l'&&s[i-1]!='r'&&s[i-2]!='i'&&s[i-3]!='g') g++;
//girl的运算
}
cout<<b<<endl<<g<<endl; //输出结果
return 0;
}
边栏推荐
- Vite3.0 release
- Hcia-r & s self use notes (8) IP address basis, subnet mask, subnet Division
- 全国职业院校技能大赛网络安全B模块 编码信息获取
- 【著色器實現Wave效果_Shader效果第一篇】
- 【无标题】
- [vulnhub] raven-2 (MySQL UDF authorization)
- Is it safe to open an account with baoguoxin securities? Can I drive it?
- 从编译器对指令集的要求看API设计原则
- 视角渲染
- 盛夏防火患!广东开展夏季消防安全检查
猜你喜欢
随机推荐
MySQL - Multi version concurrency control (mvcc)
简单聊聊最近遇到的两个面试题
25 performance testing articles
Fire prevention in midsummer! Guangdong carries out fire safety inspection in summer
[vulnhub] raven-2 (MySQL UDF authorization)
全国职业院校技能大赛网络安全B模块 编码信息获取
[untitled]
盛夏防火患!广东开展夏季消防安全检查
MySQL data table query
@How can conditionalonmissingbean cover beans in third-party components
Implementation of [priority queue (heap)] binomial queue class template
深入详细理解矩阵 (矩阵的加减乘、转置、共轭、共轭转置)
交换机与路由器技术:链路聚合、生成树协议STP和生成树协议配置
Hcia-r & s self use notes (10) VRP foundation, command, remote management
(手工)【sqli-labs62-65】限制注入次数:盲注
[cann training camp] AI CPU operator development based on shengteng cann platform
Visual Studio错误:strcpy出现C4996错误的解决
[untitled]
MySQL常用命令
1764. Connect the subarrays of another array to get an array ●●








![(manual) [sqli-labs62-65] limit injection times: blind injection](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
