E. Email Destruction---大模拟
作者:互联网
Email Destruction
Time Limit: 3 Sec Memory Limit: 512 Mb
题目链接http://acm.csu.edu.cn:20080/csuoj/problemset/problem?pid=2312
Description
You have an account on ICPCorrespondence.com. This is an email service where emails are grouped into chains by their subject as follows.
The first email in every chain has a non-empty subject consisting of lowercase English letters. Every succeeding email in the chain has a subject consisting of "Re: " followed by the subject of the previous email.
For example, if the first email in the chain has subject “subj”, then the second email has subject “Re: subj”, the third one has subject “Re: Re: subj”, and so on. Formally, the subject of the k-th email in the chain consists of "Re: " repeated k − 1 times followed by the subject of the first email in the chain.
In your mailbox, you had one or more chains of emails with unique subjects. You never removed any emails from your mailbox.
Unfortunately, one day ICPCorrespondence.com was attacked by hackers. As a result of this attack, some emails were removed from the server, while the remaining emails were shuffled.
You are not sure how many emails you had in the mailbox before the attack, but you guess that this number is n. Can you check whether this guess can be correct?
Input
The first line of the input contains two integers n and k — the number of emails that you think were in the mailbox before the attack, and the number of emails left after the attack, respectively (1 ≤ k ≤ n ≤ 100).
The following k lines contain subjects of the emails left in your mailbox, one per line. The subject of each email consists of "Re: " repeated zero or more times, followed by at least one and no more than 10 lowercase English letters. The length of each subject does not exceed 500. All email subjects are pairwise distinct.
Output
If your guess about the number of emails in your mailbox prior to the attack can be correct, output a single word “YES”. Otherwise, output a single word “NO”.
Sample Input
7 3
Re: Re: Re: hello
Re: world
hello
Sample Output
YES
Hint
In the first example, the guess can be correct. For example, you could have emails with subjects “hello”, “Re: hello”, “Re: Re: hello”, “Re: Re: Re: hello”, “Re: Re: Re: Re: hello”, “world”, and “Re: world”.
In the second example, the guess is incorrect since there had to be at least three emails in the chain of “pleasehelp” and at least one email in the chain of “me”.
emmm,题目大意,黑客攻击了你的邮箱,删去了一部分邮件,你猜你的邮箱原邮件数为n,判断是否正确。这些邮件的特点是链式的:即第一个主题如果为W则不变,下一个与之一样主题的需要重复一遍:Re: W,第三一样的就Re: Re: W依次类推(注意:后有空格)。
要使得猜到的正确,则n必须大于等于现有已知的邮件数,实际我们只需要标记一下就行了,也就是求每个主题Re的最大值+1,然后将每个主题的最大值加上就是当下已知的最大值,只要n>=该值就是Ok的。
但关键是数据的处理有点麻烦,我用string比较方便一点。。。当然,当时比赛的时候数据有点毒瘤。。。开头两个整数之后还有一个空格。。。到时getchar()一次的WA到死。。。而很不幸的是我就是这一撮人中的一个。。。最后十几分钟的时候多加了一个getchar()就过了。。。
以下是AC代码:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
char s[600];
string tp[600];
map<string,int>q;
int main()
{
int n,ans=0,k,tot=1;
scanf ("%d%d",&n,&k);
getchar();
getchar();
for (int i=1; i<=k; i++){
gets(s);
int len=strlen(s);
if (s[0]!='R') {
for (int j=0; j<len; j++) tp[tot]+=s[j];
int p=q[tp[tot]];
q[tp[tot]]=max(p,1);
tot++;
if (i==k) break;
else continue;
}
int cnt=0;
for (int j=0; j<len; j++){
if (s[j]=='R') {
cnt++; j+=3;
}
else {
cnt++;
for (int kk=j; kk<len; kk++) tp[tot]+=s[kk];
int p=q[tp[tot]];
q[tp[tot]]=max(p,cnt);
tot++;
break;
}
}
}
sort(tp+1,tp+tot);
string kw="------";
for (int i=1; i<tot; i++){
if (tp[i]!=kw) {
ans+=q[tp[i]];
kw=tp[i];
}
}
if (ans<=n) printf ("YES\n");
else printf ("NO\n");
return 0;
}
标签:chain,hello,Email,Re,email,Destruction,emails,模拟,subject 来源: https://blog.csdn.net/qq_43906000/article/details/89764391