湖南省第十一届程序设计大赛 (水题)聊天止于呵呵

模拟题老师基本上都不讲,因为只要足够细心,大家都能做对。集训到今天就结束了。这次下来,

发现自己做模拟题已经很有手感了,相较于之前,真的有很大的成长。

简单的分析下这道题,

1、首先,要找出总共有多少段对话,就想到了用map<string, int>来存对话是否出现过,A->B,B->A只要没有出现hehehe...作为结束就属于同一段对话。

2、处理对话内容,把非字母转成空格,大写转小写,然后要把对话拆成单词,看是否是由hehehe...组成,就想到了前段学的stringstream

3、确定每个单词是否是由hehehe组成,这个非常简单,

输入要稍稍注意下,因为对话有空格的所以只能用gets(),或getline()接收,(或者其它我没学的,不以空格结束的接收字符串的)

代码如下:

#include <iostream>
#include <sstream>
#include <string>
#include<cstdio>
#include<map>
#include<cstring>
#include <cmath>

using namespace std;

map<string, int> cnt;

int chi(char c[])
{
    int clen= strlen(c);
    if(clen%2!=0)
        return 0;
    else
    {
        for(int i=0; i<clen-1; i=i+2)
        {
            if(c[i]!='h'||c[i+1]!='e')
                return 0;
        }
        return 1;
    }
}
int check(char s[])
{
    int slen=strlen(s);
    for(int i=0; i<slen; i++)
    {
        if(isalpha(s[i]))
            s[i] = tolower(s[i]);
        else
            s[i] = ' ';
    }
    stringstream ss(s);
    char c[1005];
    while(ss>>c)
    {
        if(chi(c)==1)
            return 1;
    }
    return 0;
}
int main()
{
    char a, b, s[1005];
    double sum=0, cont=0;
    while((scanf("%c->%c:", &a, &b))!=EOF)
    {
        gets(s);
        string z, y;
        z += a+b;
        y += b+a;
        if((!cnt.count(z)&&!cnt.count(y))||(cnt[y]==0&&cnt[z]==0))
        {
            cnt[y]=1;
            cnt[z]=1;
            sum++;
        }
        if(check(s)==1)  //s串中有hehehe;
        {
            cont++;
            cnt[z]=0;
            cnt[y]=0;
        }
    }
    printf("%.0lf%%\n", cont/sum*100.0);
    return 0;
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值