ZOJ1315 Excuses, Excuses!(string类中find()函数的运用,很坑,看题得仔细)

题目 

题目链接:

ZOJ1315 Excuses, Excuses!icon-default.png?t=L892https://zoj.pintia.cn/problem-sets/91827364500/problems/91827364814

测试样例

输入样例

5 3
dog
ate
homework
canary
died
My dog ate my homework.
Can you believe my dog died after eating my canary... AND MY HOMEWORK?
This excuse is so good that it contain 0 keywords.
6 5
superhighway
crazy
thermonuclear
bedroom
war
building
I am having a superhighway built in my bedroom.
I am actually crazy.
1234567890.....,,,,,0987654321?????!!!!!!
There was a thermonuclear war!
I ate my dog, my canary, and my homework ... note outdated keywords?

输出样例

Excuse Set #1
Can you believe my dog died after eating my canary... AND MY HOMEWORK?

Excuse Set #2
I am having a superhighway built in my bedroom.
There was a thermonuclear war!

提交结果截图

带详细注释的源代码

#include <iostream>
#include <vector>
#include <string>
#include <cstdio>
#include <algorithm>
using namespace std;

struct Excuse
{
	string excuse;//借口
	int key_cnt;//包含的关键词的个数
};

bool cmp(Excuse a, Excuse b)
{
	return a.key_cnt > b.key_cnt;
}

//字符串中的大写字符全转小写字符
string change(string str)
{
	for (int i = 0; i < str.length(); i++)
		if ('A' <= str[i] && str[i] <= 'Z')
			str[i] = str[i] + 'a' - 'A';
	return str;
}

int main()
{
	int order = 1;//次序
	int k, e;
	vector<string>keywords;//存储关键词
	vector<Excuse>excuse;//存储借口
	vector<Excuse>excuse_copy;//保存副本,副本也可直接在结构体中定义
	while (cin >> k >> e)
	{
		string key;
		Excuse excu;
		excu.key_cnt = 0;
		keywords.clear();//清空向量
		excuse.clear();
		excuse_copy.clear();
		getchar();
		for (int i = 0; i < k; i++)
		{
			getline(cin, key);
			key = change(key);//因为不需要输出,直接大学转小写,不备份
			keywords.push_back(key);
		}

		for (int i = 0; i < e; i++)
		{
			getline(cin, excu.excuse);
			excuse.push_back(excu);
		}
		excuse_copy = excuse;//备份
		for (int i = 0; i < e; i++)
		{
			excuse[i].excuse = change(excuse[i].excuse);//大写转小写
			for (int j = 0; j < k; j++)
			{
				int start = 0;
				while ((start = excuse[i].excuse.find(keywords[j],start))!=string::npos)
				{
					start += keywords[j].length();
					//一直不能AC的原因:匹配到的字符串的下一个字符不能是字母!!!
					if (excuse[i].excuse[start] > 'z' || excuse[i].excuse[start] < 'a')
					{
						excuse[i].key_cnt++;
						break;
					}
				}
			}
			excuse_copy[i].key_cnt = excuse[i].key_cnt;//拷贝 包含的关键词的个数
		}
		vector<Excuse>::iterator it = excuse_copy.begin();
		sort(it, it + excuse_copy.size(), cmp);//按包含的关键词的个数从大到小排序
		cout << "Excuse Set #" << order<<endl;
		int max_cnt = excuse_copy[0].key_cnt;
		for (int i = 0; i < excuse_copy.size(); i++)
			if (excuse_copy[i].key_cnt == max_cnt)//如果同样最多,均输出
				cout << excuse_copy[i].excuse << endl;
			else
				break;
		cout << endl;//额外输出一行
		order++;
	}
	return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值