[H]聊天止于呵呵
问题描述
(现代版)俗话说:流言止于智者,聊天止于呵呵。输入一段聊天记录,你的任务是数一数有
多少段对话“止于呵呵”,即对话的最后一句话包含单词 hehe 或者它的变形。
具体来说,我们首先提取出对话的最后一句话,把所有非字母的字符替换成空格,把所有字符 替换成小写,然后导出一个单词列表(由空格隔开),只要列表中的任何一个单词是 hehe,这 段对话就算作“止于呵呵”。比如,"Hi! Are you OK?" 会变成四个单词:hi, are, you, ok。注 意,单词列表可以是空的(比如,这句话是:"?!?!!")
有些人喜欢使用 hehe 的变形,这些变形也应被视为“呵呵”。为了简单起见,本题只考虑由 n(n>1)个 he 连接而成的单词,比如 hehehe 或者 hehehehe。注意,以 hehe 为连续子串的其他单 词不应视为“呵呵”,比如 hehee,或者 ehehe。
每两个不同人之间的所有对话算作“一段对话”。
输入
输入仅包含一组数据,每行是一句对话,格式为:
人名1->人名2: 一句话.
每行最多包含 1000 个字符,最多 100 行。
输出
输出“止于呵呵”的对话段落所占的百分比,四舍五入到最近的整数。输入数据保证答案不会同时和两个整数最近。
样例输入
A->B: Hello!
A->C: Hi!
B->A: Hehe
B->D: Hei!
D->B: How are you?
A->C: Hi???
A->C: Are you there?
B->D: Hehehei!
D->B: What does hehehei mean?
F->E: I want to hehehehehe yah.
样例输出
50%
题目描述:判断每段对话的最后一句是否含有he或者hehehe(n个he)这种单词,求得有这种特性的对话占总对话的比
例。
解题思路:此题属于简单模拟题,这里各种处理很适合stl的发挥,可以用map来储存对话人物和对话内容,将map
的key设置成set,这样就保证了输入完成时,map存储的是每段对话的最后一句话,set由于能进行自动排序,直接将两个人的名字放进去,然后map的value就存储谈话内容,这样就有效的分割出要处理的串了,在判断hehe特性时,可以利用stringstream这个子串输入输出流来分解谈话内容,用函数判断单词是不是hehe特性。
STL真强大,佩服!
小提示:这里用到了正则scanf("%[^0]0%..",str..);表示的是子串输入到0字符时停止输入,后面的0代表的是去掉
该字符,但是最后的字符不会处理scanf("%[^\n]\n",str);但是最后的\n字符处理不了,所以在循环中就用getchar()由于没有oj提交此代码,如若发现错误,acmer直接指出,弱弱会修改。
抱歉之前的代码有个小bug,题意没审清楚,改了,a了。
<span style="color:#666666;">#include <cstdio> #include <cstring> #include <iomanip> #include <cmath> #include <cstdlib> #include <iostream> #include <sstream> #include <string> #include <set> #include <map> #include <stack> #include <list> #include <utility> #include <vector> #include <algorithm> #include <queue> using namespace std; typedef __int64 LL; const double PI = acos(-1); const double eps = 1e-8; const int INF = 0x3f3f3f3f; const int MAXM = 1000000; const int MAXN = 20000+10; const int MOD = 1000000007; template<typename T> inline T minn(T a,T b,T c){return (a=(a>b)?b:a)>c?c:a;} template<typename T> inline T maxx(T a,T b,T c){return (a=(a>b)?a:b)>c?a:c;} #define CLR(a,b) memset((a),(b),sizeof(a)) char str1[MAXN]; char str2[MAXN]; char str3[MAXN]; int cnt=0; int sum=0; map<set<string>,string> ma; bool jdhehe(string s) { int len = s.size(); if(len%2==1) return false; for(int i=1;i < len;i+=2) { if((s[i-1]!='H'&&s[i-1]!='h')||(s[i]!='e'&&s[i]!='E')) return false; } return true; } int main() { // freopen("datah.in","r",stdin); ios::sync_with_stdio(false); while(~scanf("%[^-]->%[^:]: %[^\n]",str1,str2,str3))//完成输入 { getchar(); set<string> s; int len3 = strlen(str3); for(int i = 0;i < len3;++ i) if(!isalpha(str3[i])) str3[i]=' '; s.insert(str1); s.insert(str2); ma[s]=str3; } string s; stringstream in; sum = ma.size(); map<set<string>,string>::iterator it = ma.begin(); for(;it!=ma.end();++ it) { in.clear(); in<<it->second; bool flag=false; while(in>>s)//拆分成单词 { if(jdhehe(s)) { flag=true; break; } } if(flag) cnt++; } double ans = (1.0*cnt/sum)*100; printf("%.0lf%%", ans); }</span><span style="color:#cc6600;"> </span>