查询c++题解

题目:`查询
题目描述

老师给小Z布置了一项作业,老师给了小Z一本英语词典,书上有很多个句子,每个句子是由字母、空格以及“,”,每个句子都以“.”或“?”结尾。老师给出一个词汇,让小Z统计这些词汇在每个句子中出现的次数。

输入

若干行由字母、空格组成的句子,句子以“.”或“?”为结尾,每行只有一个句子。
最后一行以ask:开始的句子,是老师询问的词汇。

输出

若干行,输出每个句子的编号和词汇出现次数。

样例输入

【样例输入1】
Hello my name is Peter what is your name?
This is my friend Jack.
ask:is

【样例输入2】
How much wood would a woodchuck chuck If a woodchuck could chuck wood?
He would chuck the wood as much as he could If a woodchuck could chuck wood.
ask:wood

样例输出

【样例输出1】
1 2
2 1

【样例输出2】
1 2
2 2

数据范围限制

【数据规模和约定】
句子数量≤200,每个句子的长度Li不超过200,
查询的词汇长度不超过10,注意区分大小写。

提示

【输入输出样例1 说明】
Hello,my name is Peter,what is your name?
This is my friend Jack.
第一个句子有2个is,第二个句子有1个is。

【输入输出样例2 说明】
How much wood would a woodchuck chuck If a woodchuck could chuck wood?
He would chuck the wood as much as he could If a woodchuck could chuck wood.
第一个句子有2个wood,第二个句子有2个wood
来源/作者: Jerome
Cake 1#

#include<cstdio>
#include<cstring>
char tmp[201][201],ss[200],s[210];
int len,x,ans;
bool p;
int main()
{
	gets(s);
	while(s[0]!='a'||s[1]!='s'||s[2]!='k'||s[3]!=':')
	{
		x++;
		strcpy(tmp[x],s);
		gets(s);
	}
	len=-1;
	for(int i=4;i<strlen(s);i++)
	{
		ss[++len]=s[i];
	}
	len=strlen(ss);
	for(int i=1;i<=x;i++)
	{
		for(int j=0;j<strlen(tmp[i]);j++)
		{
			if(tmp[i][j]==ss[0]&&(tmp[i][j-1]==' '||j==0))
			{
				p=true;
				for(int k=j;k<=j+len-1;k++)
				{
					if(ss[k-j]!=tmp[i][k])
					{
						p=false;
						break;
					}
				}
				if(p==true&&(tmp[i][j+len]==' '||tmp[i][j+len]=='?'||tmp[i][j+len]=='.'))
				{
					ans++;
				}
			}
		}
		printf("%d %d\n",i,ans);
		ans=0;
	}
	return 0;
}

Cake 2#

#include<bits/stdc++.h>
using namespace std;
string k,l;
int n,cnt=0,ans=0,xun=0,xun2=0;
char s[30000+10][100+10],o[5]={"ask"},*y;
bool flag=false;
int main(){
	while(cin>>s[++cnt]){
		char *u=strtok(s[cnt]," :");
		if(strcmp(u,o)==0)
			flag=true;
		u=strtok(NULL," ");
		y=u;
		if(flag)
			break;
	}
	for(int i=0;i<cnt;++i){
		int k=strlen(s[i]);
		xun2=xun;
		if(s[i][k-1]=='.'||s[i][k-1]=='?')
			xun++;
		char *p=strtok(s[i]," ?."); 
		while(p!=NULL) {
			if(strcmp(p,y)==0)
				ans++;
			p=strtok(NULL," ?.");
		}
		if(xun-xun2>0){
			printf("%d %d\n",xun,ans);
			ans=0;
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值