2016北京赛区网赛C(模拟)

本文介绍了一种通过字符串处理和模拟的方式统计文本中特定短语出现频率的算法。该算法利用了C++标准库中的字符串操作及Map容器来实现短语的识别与计数,并找出出现次数最多的短语。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思考:
1、别忘了清空数组等
2、想好了再写

代码写得比较冗余。其实主要代码段就是

int l = strlen (s);
        for (int i = 0; i <= l; i++) {
            if (s[i] == ',' || s[i] == '.' || s[i] == '\0') {
                if (flag1 == 2) {
                    string s2 = first;
                    s2 += ' ';
                    s2 += second;
                    //cout << first << " " << second << " " << s2 << endl;
                    //cout << flag1 << " " << flag2 << endl;
                    if (mp.count (s2)) {
                        mp[s2]++;
                    } else {
                        mp[s2] = 1;
                    }
                    first.clear();
                    second.clear();
                    maxnum = max (maxnum, mp[s2]);
                }
                first.clear();
                flag1 = 1;
                flag2 = 1;
            } else if (s[i] == ' ') {
                if (flag2) continue;
                if (i != 0 && s[i - 1] == ' ') continue;
                if (flag1 == 1) {
                    flag1 = 2;
                } else if (flag1 == 2) {
                    string s2 = first;
                    s2 += ' ';
                    s2 += second;
                    //cout << s2 << endl;
                    //cout << flag1 << " " << flag2 << endl;
                    if (mp.count (s2)) {
                        mp[s2]++;
                    } else {
                        mp[s2] = 1;
                    }
                    first = second;
                    second.clear();
                    maxnum = max (maxnum, mp[s2]);
                }

            } else {
                flag2 = 0;
                if (flag1 == 1) first += s[i];
                if (flag1 == 2) second += s[i];
            }
        }

用Map存储所有的短语出现过的次数,最后扫一遍找出现最多次的短语就好了。
上面的哪一段代码实现的功能是把所有的短语都加入到map里。这道题是纯模拟,比较麻烦,不过处理好了短语之后,也就好做多了。

#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <queue>

using namespace std;
const int maxn = 600;
map<string, int> mp;

int main()
{
    #ifndef ONLINE_JUDGE
    freopen ("in.txt", "r", stdin);
    #endif // ONLINE_JUDGE
    char s[maxn];
    while (gets (s)) {
        int maxnum = 0;
        mp.clear();
        int flag1 = 1;
        int flag2 = 1;
        string first, second;
        first.clear();
        second.clear();
        int l = strlen (s);
        for (int i = 0; i <= l; i++) {
            if (s[i] == ',' || s[i] == '.' || s[i] == '\0') {
                if (flag1 == 2) {
                    string s2 = first;
                    s2 += ' ';
                    s2 += second;
                    //cout << first << " " << second << " " << s2 << endl;
                    //cout << flag1 << " " << flag2 << endl;
                    if (mp.count (s2)) {
                        mp[s2]++;
                    } else {
                        mp[s2] = 1;
                    }
                    first.clear();
                    second.clear();
                    maxnum = max (maxnum, mp[s2]);
                }
                first.clear();
                flag1 = 1;
                flag2 = 1;
            } else if (s[i] == ' ') {
                if (flag2) continue;
                if (i != 0 && s[i - 1] == ' ') continue;
                if (flag1 == 1) {
                    flag1 = 2;
                } else if (flag1 == 2) {
                    string s2 = first;
                    s2 += ' ';
                    s2 += second;
                    //cout << s2 << endl;
                    //cout << flag1 << " " << flag2 << endl;
                    if (mp.count (s2)) {
                        mp[s2]++;
                    } else {
                        mp[s2] = 1;
                    }
                    first = second;
                    second.clear();
                    maxnum = max (maxnum, mp[s2]);
                }

            } else {
                flag2 = 0;
                if (flag1 == 1) first += s[i];
                if (flag1 == 2) second += s[i];
            }
        }
        while (gets (s)) {
            if (s[0] == '#') break;
            int flag1 = 1;
            int flag2 = 1;
            string first, second;
            int l = strlen (s);
            for (int i = 0; i <= l; i++) {
                if (s[i] == ',' || s[i] == '.' || s[i] == '\0') {
                    if (flag1 == 2) {
                        string s2 = first;
                        s2 += ' ';
                        s2 += second;
                        //cout << s2 << endl;
                        //cout << flag1 << " " << flag2 << endl;
                        if (mp.count (s2)) {
                            mp[s2]++;
                        } else {
                            mp[s2] = 1;
                        }
                        first.clear();
                        second.clear();
                        maxnum = max (maxnum, mp[s2]);
                    }
                    first.clear();
                    flag1 = 1;
                    flag2 = 1;
                } else if (s[i] == ' ') {
                    if (flag2) continue;
                    if (i != 0 && s[i - 1] == ' ') continue;
                    if (flag1 == 1) {
                        flag1 = 2;
                    } else if (flag1 == 2) {
                        string s2 = first;
                        s2 += ' ';
                        s2 += second;
                       // cout << s2 << endl;
                       // cout << flag1 << " " << flag2 << endl;
                        if (mp.count (s2)) {
                            mp[s2]++;
                        } else {
                            mp[s2] = 1;
                        }
                        first = second;
                        second.clear();
                        maxnum = max (maxnum, mp[s2]);
                    }
                    flag2 = 1;
                } else {
                    flag2 = 0;
                    if (flag1 == 1) first += s[i];
                    if (flag1 == 2) second += s[i];
                }
            }
        }
        map<string, int>::iterator it;
        for (it = mp.begin(); it != mp.end(); it++) {
            if (it->second == maxnum) {
                cout << it->first << ":" << maxnum << endl;
                break;
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值