csu1783——聊天止於呵呵(字符串)

Description
(現代版)俗話說:流言止於智者,聊天止於呵呵。輸入一段聊天記錄,你的任務是數一數有

多少段對話“止於呵呵”,即對話的最後一句話包含單詞 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%

可以確定的是hehe及其變種一定只存在對話的最後一句,所以只要分析有多少段對話,再把hehe出現的次數與之相除就行了

#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cstdio>
#include <set>
#include <math.h>
#include <algorithm>
#include <queue>
#include <iomanip>
#include <cctype>
#define INF 0x3f3f3f3f
#define MAXN 10000005
#define Mod 1000000007
using namespace std;
set<pair<string,string> > table;
bool check(string s)
{
    if(s.length()<4)
        return false;
    string tmp="he",s1="";
    int len=s.length()/2;
    for(int i=1; i<=len; ++i)
        s1+=tmp;
    if(s1==s)
        return true;
    else
        return false;
}
int main()
{
    string s;
    double ans=0;
    table.clear();
    while(getline(cin,s))
    {
        s+='#';
        string name1="",name2="";
        int i=0,len=s.length();
        while(s[i]!='-')
        {
            name1+=s[i];
            i++;
        }
        i+=2;
        while(s[i]!=':')
        {
            name2+=s[i];
            i++;
        }
        if(name1<name2)
            swap(name1,name2);
        if(table.find(make_pair(name1,name2))==table.end())
            table.insert(make_pair(name1,name2));
        i+=2;
        string word="";
        for(; i<len; ++i)
        {
            if((s[i]>='A'&&s[i]<='Z')||(s[i]>='a'&&s[i]<='z'))
                word+=tolower(s[i]);
            else
            {
                if(check(word))
                {
                    ans++;
                    break;
                }
                word="";
            }
        }
    }
    ans=ans/table.size();
    ans*=100;
    printf("%d%%\n",int(ans));
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章