Codeforces Round #374 (Div. 2) B. Passwords —— 基础题
来源:互联网 发布:大富翁数据库 编辑:程序博客网 时间:2024/06/11 14:12
题目链接:http://codeforces.com/contest/721/problem/B
Vanya is managed to enter his favourite site Codehorses. Vanya uses n distinct passwords for sites at all, however he can't remember which one exactly he specified during Codehorses registration.
Vanya will enter passwords in order of non-decreasing their lengths, and he will enter passwords of same length in arbitrary order. Just when Vanya will have entered the correct password, he is immediately authorized on the site. Vanya will not enter any password twice.
Entering any passwords takes one second for Vanya. But if Vanya will enter wrong password k times, then he is able to make the next try only 5 seconds after that. Vanya makes each try immediately, that is, at each moment when Vanya is able to enter password, he is doing that.
Determine how many seconds will Vanya need to enter Codehorses in the best case for him (if he spends minimum possible number of second) and in the worst case (if he spends maximum possible amount of seconds).
The first line of the input contains two integers n and k (1 ≤ n, k ≤ 100) — the number of Vanya's passwords and the number of failed tries, after which the access to the site is blocked for 5 seconds.
The next n lines contains passwords, one per line — pairwise distinct non-empty strings consisting of latin letters and digits. Each password length does not exceed 100 characters.
The last line of the input contains the Vanya's Codehorses password. It is guaranteed that the Vanya's Codehorses password is equal to some of his n passwords.
Print two integers — time (in seconds), Vanya needs to be authorized to Codehorses in the best case for him and in the worst case respectively.
5 2cbaabcbb1abCABCabc
1 15
4 10011221222
3 4
Consider the first sample case. As soon as all passwords have the same length, Vanya can enter the right password at the first try as well as at the last try. If he enters it at the first try, he spends exactly 1 second. Thus in the best case the answer is 1. If, at the other hand, he enters it at the last try, he enters another 4 passwords before. He spends 2 seconds to enter first 2 passwords, then he waits 5seconds as soon as he made 2 wrong tries. Then he spends 2 more seconds to enter 2 wrong passwords, again waits 5 seconds and, finally, enters the correct password spending 1 more second. In summary in the worst case he is able to be authorized in 15 seconds.
Consider the second sample case. There is no way of entering passwords and get the access to the site blocked. As soon as the required password has length of 2, Vanya enters all passwords of length 1 anyway, spending 2 seconds for that. Then, in the best case, he immediately enters the correct password and the answer for the best case is 3, but in the worst case he enters wrong password of length 2 and only then the right one, spending 4 seconds at all.
题解:
1.首先,用string肯定比char方便很多。其次,由于长度最大只为100, 所以可以开个 len[100]数组,记录每种长度下有多少个字符串,(特例:passwords只记录一次,因为一遇到passwords就结束了)。
2.不管最大值还是最小值,都需要将长度小于passwords的字符串走完,对于最小情况,走的第一个长度等于passwords的字符,即为passwords; 对于最大情况, 走的最后一个长度等于passwords的字符,即为passwords。
细节:当到达终点的那一刻,如果正好走了k*i步,他也不需要罚时,因为已经到达终点了,需要特判一下。
代码如下:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <string>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <sstream>#include <algorithm>using namespace std;#define ms(a, b) memset((a), (b), sizeof(a))typedef long long LL;const double eps = 1e-6;const int INF = 2e9;const LL LNF = 9e18;const int mod = 1e9+7;const int maxn = 100+10;int n, len[maxn], k;string s[maxn], x;void init(){ cin>>n>>k; for(int i = 1; i<=n; i++) cin>>s[i]; cin>>x;}void solve(){ int B = 0; for(int i = 1; i<=n; i++) { if(s[i]==x) { if(!B) len[x.size()]++, B = 1; } else len[s[i].size()]++; } int sum = 0; for(int i = 1; i<x.size(); i++) sum += len[i]; int t1 = sum + 1; int minn = t1 + (t1/k)*5; if(t1%k==0) minn -= 5; int t2 = sum + len[x.size()]; int maxx = t2 + (t2/k)*5; if(t2%k==0) maxx -= 5; printf("%d %d\n",minn, maxx);}int main(){// int T;// scanf("%d",&T);// while(T--) { init(); solve(); } return 0;}
- Codeforces Round #374 (Div. 2) B. Passwords —— 基础题
- Codeforces Round #374 (Div. 2) B. Passwords 贪心
- Passwords Codeforces Round#374-B
- Codeforces #374(Div.2)B. Passwords【模拟】
- Codeforces Round #374 (Div. 2) A&B
- Codeforces Round #169 (Div. 2) B题
- Codeforces Round #188 (Div. 2) B题
- Codeforces Round #218 (Div. 2) B题
- Codeforces Round #238 (Div. 2) B题
- Codeforces Round #402 (Div. 2) B题
- Codeforces Round #404 (Div. 2) B题
- Codeforces Round #446 (Div. 2) B 题
- CodeForces 581B Luxurious Houses(简单题)——Codeforces Beta Round #322 (Div. 2)
- Codeforces Round #154 (Div. 2)——B
- Codeforces Round #156 (Div. 2)——B
- Codeforces Round #151 (Div. 2)——B
- Codeforces Round #152 (Div. 2)——B
- Codeforces Round #149 (Div. 2)——B
- 《利用Python进行数据分析》第二章-学习笔记
- 使用回调函数实现socket服务器端子线程刷新UI数据(socket通信第三弹)
- POJ 1001 Exponentiation 大浮点数乘法
- 解决Windows 7启动 Docker 报错
- lamp环境的搭建(源码包安装)
- Codeforces Round #374 (Div. 2) B. Passwords —— 基础题
- 执行转换时如何让Kettle记录错误并继续执行?——记一种解决方案
- caffeOnSpark install in ubuntu14.04
- centos 关闭防火墙及hostname设置
- base64
- 转载redis之jedis简单使用
- Go语言学习(一)下载安装Go编译器,并配置环境变量
- [LeetCode]16.threeSumClosest
- OpenJ_Bailian 2980 || POJ 2389 大整数乘法