(现代版)俗话说:流言止于智者,聊天止于呵呵。输入一段聊天记录,你的任务是数一数有

多少段对话“止于呵呵”,即对话的最后一句话包含单词 hehe 或者它的变形。

具体来说,我们首先提取出对话的最后一句话,把所有非字母的字符替换成空格,把所有字符 替换成小写,然后导出一个单词列表(由空格隔开),只要列表中的任何一个单词是 hehe,这 段对话就算作“止于呵呵”。比如,”Hi! Are you OK?” 会变成四个单词:hi, are, you, ok。注 意,单词列表可以是空的(比如,这句话是:”?!?!!”)

有些人喜欢使用 hehe 的变形,这些变形也应被视为“呵呵”。为了简单起见,本题只考虑由 n(n>1)个 he 连接而成的单词,比如 hehehe 或者 hehehehe。注意,以 hehe 为连续子串的其他单 词不应视为“呵呵”,比如 hehee,或者 ehehe。

每两个不同人之间的所有对话算作“一段对话”。

Input
输入仅包含一组数据,每行是一句对话,格式为:
人名1->人名2: 一句话.
每行最多包含 1000 个字符,最多 100 行。
Output
输出“止于呵呵”的对话段落所占的百分比,四舍五入到最近的整数。输入数据保证答案不会
同时和两个整数最近。
Sample Input
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.
Sample Output
50%
Hint
样例解释
A 和 B 之间的最后一句话是”Hehe”.
A 和 C 之间的最后一句话是”Are you there?”.
B 和 D 之间的最后一句话是”What does hehehei mean?”.
E 和 F 之间的最后一句话是”I want to hehehehehe yah”. 最后第一段和最后一段话是“止于呵呵”的(注意最后一段对话是以呵呵的变种结束),因此 比例是 50%。
分析:
字符串处理问题,可以考虑使用STL,会使编码容易得很多;
代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<map>
#include<set>
#include<sstream> using namespace std;
map<pair<char,char> ,string >mp;
set<string> s;
void Init(){
string tmp="hehe";
for(int i=;i<=;i++){
s.insert(tmp);
tmp+="he";
}
}
bool stop_hehe(string &str){
stringstream ss(str);
string tmp;
string::iterator it;
while(ss >> tmp){
if(s.count(tmp)) return true;
}
return false;
}
int main(){
string str;
Init();
while(getline(cin,str)){
char p = min(str[],str[]),q = max(str[],str[]);
int len = str.size();
for(int i=;i<len;i++) str[i] = tolower(str[i]);
mp[make_pair(p,q)] = str;
}
double total=,stop=;
int ans;
map<pair<char,char>,string>::iterator it;
for(it =mp.begin();it!=mp.end();it++){
total++;
if(stop_hehe((*it).second))stop++;
}
double tmp = stop/total;
ans = (int)((tmp+0.005)*100.0);
printf("%d%%\n",ans);
}

T-聊天止于呵呵的更多相关文章

  1. NOJ——聊天止于呵呵(string流重定向+map,水题)

    [1645] 聊天止于呵呵 时间限制: 5000 ms 内存限制: 65535 K 问题描述 (现代版)俗话说:流言止于智者,聊天止于呵呵.输入一段聊天记录,你的任务是数一数有 多少段对话“止于呵呵” ...

  2. 深入理解java虚拟机(十二) Java 语法糖背后的真相

    语法糖(Syntactic Sugar),也叫糖衣语法,是英国计算机科学家彼得·约翰·兰达(Peter J. Landin)发明的一个术语.指的是,在计算机语言中添加某种语法,这些语法糖虽然不会对语言 ...

随机推荐

  1. python-魔法属性和反射

    python魔法属性和反射 #!/usr/bin/python3 # coding:utf-8 # Auther:AlphaPanda # Description:与类相关的魔法属性 # Versio ...

  2. 分组统计 over(partition by

    sum( CASE WHEN ISNULL(b.zl, 0) = 0 THEN C.LLZL ELSE b.zl END * c.pccd * b.sl) over(partition by b.dj ...

  3. vue学习-day05 -- 案例:名字合并(监听data数据的改变)

    1.案例:名字合并(监听data数据的改变) 使用keyup事件监听data数据的改变 <!DOCTYPE html> <html> <head> <titl ...

  4. 策略模式------《Head First 设计模式》

    第一章---策略模式 xzmxddx 学习方式:书籍<Head First 设计模式>,这本书通俗易懂,所有知识点全部取自本书. 面向对象设计原则 封装变化 多用组合,少用继承 针对接口编 ...

  5. eclipse中使用maven搭建多模块项目

    暂时参考:https://blog.csdn.net/u012343297/article/details/79883870

  6. React 项目使用 React-router-dom 4.0 以上版本时使用 HashRouter 怎么控制 history

    不添加 history 时,来回点击 Link 会在控制台报错如下 Warning: Hash history cannot PUSH the same path; a new entry will ...

  7. Python3学习笔记(七):字典

    在python中,有一种通过名字来引用值的数据结构,这种类型的数据结构成为映射. 字典是Python中唯一内建的映射类型,具有以下特点: 字典中的值是无序的 值存在特定的键(key)下 键(key)可 ...

  8. Codeforces Round #403---C题(DFS,树)

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

  9. vue 中 event.stopPropagation() 和event.preventDefault() 使用

    1.event.stopPropagation()方法 这是阻止事件的冒泡方法,不让事件向document上蔓延,但是默认事件任然会执行,当你掉用这个方法的时候,如果点击一个连接,这个连接仍然会被打开 ...

  10. Spring Data JPA学习笔记

    下面先来介绍一下JPA中一些常用的查询操作: //And --- 等价于 SQL 中的 and 关键字,比如 findByHeightAndSex(int height,char sex): publ ...