// 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;
}
敏感词处理
最新推荐文章于 2024-11-19 18:39:25 发布