敏感词处理

// test9.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <cstring>
#include <vector>
#include <string>

using namespace std;
const int Char_Max=150;//字符串存储长度最大值

//string trim(string s)   
//{  
//    //string s = const_cast<string>(s1);
//    if (s.empty())   
//    {  
//        return s;  
//    }  
//    s.erase(0,s.find_first_not_of(" "));  
//    s.erase(s.find_last_not_of(" ") + 1);  
//    return s;  
//}  
string trim(string s)
{
    const string &space =" \f\n\t\r\v" ;
    string r=s.erase(s.find_last_not_of(space)+1);
    return r.erase(0,r.find_first_not_of(space));
}

//
//string trim(const string* s1)
//{
//    string *s = const_cast<string*>(s1);
//    const string &space =" \f\n\t\r\v" ;
//    string r=s->erase(s->find_last_not_of(space)+1);
//    return r.erase(0,r.find_first_not_of(space));
//}
bool kmp(const string &needle, const string &Target)
{
    string strTarget = trim(Target);
    int m = needle.size();
    vector<int> border(m);
    border[0] = 0;

    for (int i = 1; i < m; ++i) {
        border[i] = border[i - 1];
        while (border[i] > 0 && needle[i] != needle[border[i]])
            border[i] = border[border[i] - 1];
        if (needle[i] == needle[border[i]]) border[i]++;
    }

    int n = strTarget.size();
    int seen = 0;
    for (int i = 0; i < n; ++i){
        while (seen > 0 && strTarget[i] != needle[seen])
            seen = border[seen - 1];
        if (strTarget[i] == needle[seen]) seen++;
        if (seen == m) return true; // Ocurre entre [i - m + 1, i]
    }
    return false;
}

std::wstring trim(const std::wstring& s, const std::wstring& drop)
{
    std::wstring src = s;
    // trim right
    std::wstring r=src.erase(s.find_last_not_of(drop)+1);
    // trim left
    return r.erase(0,r.find_first_not_of(drop));
}

int _tmain(int argc, _TCHAR* argv[])
{
    string text = "中国共  产党";
    string pattern = "共产党";
    if (kmp(pattern, text))
    {
        cout<<"包含敏感词!!"<<endl;
    }
    //return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值