zoukankan      html  css  js  c++  java
  • codeforces Gym 100338F Spam Filter 垃圾邮件过滤器(模拟,实现)

    阅读题,

    概要:给出垃圾邮件和非垃圾邮件的集合,然后按照题目给出的贝叶斯公式计算概率一封邮件是垃圾邮件的概率。

    逐个单词判断,将公式化简一下就是在垃圾邮件中出现的次数和在总次数的比值,大于二分之一就算是垃圾邮件的单词。

    如果一个邮件里的垃圾邮件的单词出现次数超过给定比例就算是垃圾邮件。

    需要注意的地方是:一个邮件里单词只能统计一次,用set保证不重复。

    因为sstream会很慢,容易T,所以手写了一个类似的sstream的函数

    #include<bits/stdc++.h>
    using namespace std;
    
    typedef long double ld;
    map<string,int> spam,good;
    set<string> once;
    int s,g,n,T;
    const int maxn = 205;
    char str[maxn],word[maxn];
    
    int sscan_l(char *w,char *&s)
    {
        while(isalpha(*s) == 0){
            if(!*s) return 0;
            s++;
        }
        int j = 0;
        for(int t; s[j] && (t = isalpha(s[j])) !=0; j++){
            w[j] = s[j];
            if(t == 1) w[j] += 'a'-'A';
        }
        w[j] = ''; s += j;
        return j;
    }
    
    bool judge()
    {
        gets(str); char *p = str;
        int cnt = 0;
        once.clear();
        while(sscan_l(word,p)){
            string s0 = word;
            if(once.count(s0) == 0){
                once.insert(s0);
                int wS = spam[s0];
                int wTot = wS + good[s0];
                if(wTot && wS*2 >= wTot) cnt++;
            }
        }
    
        return cnt*100>=T*once.size();
    }
    
    int main()
    {
        freopen("spam.in","r",stdin);
        freopen("spam.out","w",stdout);
        scanf("%d%d%d%d
    ",&s,&g,&n,&T);
        for(int i = 0; i < s; i++){
            gets(str); char *p = str;
            once.clear();
            while(sscan_l(word,p)){
                string s0 = word;
                if(once.count(s0) == 0){
                    once.insert(s0);
                    spam[s0]++;
                }
            }
        }
        for(int i = 0; i < g; i++){
            gets(str); char *p = str;
            once.clear();
            while(sscan_l(word,p)){
                string s0 = word;
                if(once.count(s0) == 0){
                    once.insert(s0);
                    good[s0]++;
                }
            }
        }
        for(int i = 0; i < n; i++){
            if(judge()) puts("spam");
            else puts("good");
        }
        return 0;
    }
  • 相关阅读:
    Android 使用 DownloadManager 管理系统下载任务的方法
    移动互联网时代:你的厕所文学是什么?
    zoj 3777 Problem Arrangement(壮压+背包)
    25个增强iOS应用程序性能的提示和技巧(0基础篇)
    Oracle 同义词
    Oracle loop、while、for循环
    Oracle 序列
    Oracle 视图
    Oracle 集合操作
    Oracle 伪列
  • 原文地址:https://www.cnblogs.com/jerryRey/p/4748794.html
Copyright © 2011-2022 走看看