{"id":57,"date":"2022-11-16T17:37:11","date_gmt":"2022-11-16T09:37:11","guid":{"rendered":"http:\/\/joson.cc:8888\/?p=57"},"modified":"2022-11-19T00:05:56","modified_gmt":"2022-11-18T16:05:56","slug":"c%e5%ae%9e%e7%8e%b0%e7%9a%84rmm%e5%88%86%e8%af%8d","status":"publish","type":"post","link":"https:\/\/joson.cc\/?p=57","title":{"rendered":"C++\u5b9e\u73b0\u7684RMM\u5206\u8bcd"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">#include &lt;map>\n#include &lt;vector>\n#include &lt;list>\n#include &lt;string>\n#include &lt;algorithm>\n#include \"tools.h\"\nusing namespace std;\n\n\n#ifndef PARTH\n#define PARTH\n\n#define _ARRAY_COUNT_(x) sizeof(x)\/sizeof(*x)\n#define _WPRINTF_(c) setlocale(LC_ALL,\"chs\"); wprintf( c ); cout &lt;&lt; endl;\n#define _WVECTOR_ vector&lt;wstring>\n\nvoid CreateVectorForArray(const wstring *arr, const int arr_len, vector&lt;wstring> &amp;vec);\nvoid StrReplace(wstring &amp;str, const wstring find, const wstring replace);\n\n#define _WCreateVectorForArray_(src,target) CreateVectorForArray(src,_ARRAY_COUNT_(src),target);\n#define _WInVectory(str, vec) WInVectory(str,vec)\n\nvoid CreateVectorForArray(const wstring *arr, const int arr_len, vector&lt;wstring> &amp;vec){\n\tvector&lt;wstring> _tmp( arr, arr+arr_len );\n\tvec.resize(arr_len);\n\tcopy(_tmp.begin(), _tmp.end(), vec.begin());\n}\n\n\nvoid StrReplace(wstring &amp;str, const wstring find, const wstring replace){\n\tstring::size_type pos = 0;\n\twhile ( (pos = str.find(find, pos)) != wstring::npos ) {\n\t\tstr.replace( pos, find.size(), replace );\n\t\tpos++;\n\t}\n}\n\n \ntypedef struct WordPartResultItem{\n\tint count;\n\tint startOffset;\n\tint endOffset;\n\tint sort;\n\tfloat scale;\n\tWordPartResultItem():count(0),startOffset(0),endOffset(0),scale(0),sort(0){};\n} _WordPartResultItem;\n\n\n\ntypedef WordPartResultItem _WordPartResult;\n\n\n\nclass WordPart{\n\t\npublic:\n\t\n\tWordPart( wstring word[], int word_len);\n\t\n\tvoid SetContent( wstring content);\n\n\t\/\/\u5206\u8bcd\u65f6\u662f\u5426\u5141\u8bb8\u5c06\u6bcf\u4e2a\u5355\u5b57\u5206\u5272\u8bcd\n\tbool is_single;\n\n\t\/\/\u6267\u884c\u5206\u8bcd\n\tmap&lt;wstring,_WordPartResult> Part(); \n\nprivate:\n\n\t\/\/\u7528\u4e8e\u5206\u8bcd\u7684\u8bcd\u5178\n\tmap&lt;int,list&lt;wstring>> words;\n\t\n\t\/\/\u8bcd\u5178\u603b\u6570\n\tint words_length;\n\n\t\/\/\u5c06\u8981\u5206\u8bcd\u7684\u5185\u5bb9\n\twstring content;\n\n\t\/\/\u6700\u5927\u5339\u914d\u8bcd\u7684\u957f\u5ea6\n\tint max_length;\n\n\t\/\/\u5ffd\u7565\u7684\u5b57\u7b26\n\t_WVECTOR_ trim_str;\n\t\n\t\/\/\u65ad\u53e5\u7b26\u53f7\n\t_WVECTOR_ end_dot;\n\n\t\/\/\u83b7\u53d6\u6700\u5927\u5339\u914d\u8bcd\u7684\u957f\u5ea6\n\tint GetMaxLength(wstring *words);\n\n\tvoid setResult( map&lt;wstring,_WordPartResult> &amp;result, int &amp;sort, const wstring k, const int offset );\n\n};\n\n#endif;\n\n\nWordPart::WordPart( wstring word[], int word_len){\n\t\/\/this->words = word;\n\tthis->words_length = word_len;\n\tthis->is_single = false;\n\tmax_length = GetMaxLength(word);\n\t\n\twstring trim_tmp[] = {L\"\\\"\",L\"'\",L\"\u2018\",L\"\u2019\",L\"\u201c\",L\"\u201d\",L\"\\\\\",L\"(\",L\")\",L\"\uff08\",L\"\uff09\"};\n\twstring end_tmp[] = {L\".\",L\",\",L\"\u3002\",L\"\uff0c\",L\":\",L\"\uff1a\",L\" \"};\n\t_WCreateVectorForArray_(trim_tmp,trim_str)\n\t_WCreateVectorForArray_(end_tmp,end_dot)\n\t\n\t\/\/\u6574\u7406\u8bcd\u5178\n\tfor( int i=0; i&lt;word_len; i++){\n\t\tint k = word[i].size();\n\t\tif( words.find(k) == words.end() ){\n\t\t\tlist&lt;wstring> v;\n\t\t\twords.insert( map&lt;int,list&lt;wstring>>::value_type(k,v) );\n\t\t}\n\t\twords[k].push_back(word[i]);\n\t}\n\t\n}\n\nvoid WordPart::SetContent( wstring content){\n\t_WVECTOR_::iterator iter;\n\tfor( iter=trim_str.begin(); iter!=trim_str.end(); iter++){\n\t\tStrReplace(content,*iter,L\" \");\n\t}\n\tthis->content = content;\n}\n\nint WordPart::GetMaxLength(wstring *words){\n\t\n\tint size = 0;\n\tfor( int i=0; i&lt;words_length; i++ ){\n\t\tif( words[i].length() > size )\n\t\t\tsize = words[i].length();\n\t}\n\n\treturn size;\n\t\n}\n\n\nmap&lt;wstring,_WordPartResult> WordPart::Part(){\n\n\tmap&lt;wstring,_WordPartResult> result;\n\t\n\tif( content.empty() || words_length == 0 )\n\t\treturn result;\n\t\n\t\/\/\u5982\u679c\u8981\u6c42\u5207\u5206\u5355\u5b57\n\tif( is_single ){\n\t\twstring cstr;\n\t\twstring eng;\n\t\tint sort = 0;\n\t\tfor( int i=0; i&lt;content.size(); i++ ){\n\t\t\tcstr = content.substr(i,1);\n\t\t\tif( (cstr &lt; L\"a\" || cstr > L\"z\") &amp;&amp; (cstr &lt; L\"A\" || cstr > L\"Z\") &amp;&amp; (cstr &lt; L\"0\" || cstr > L\"9\") ){\n\t\t\t\tif(!eng.empty()){\n\t\t\t\t\tif( words.find(eng.size()) == words.end() \n\t\t\t\t\t\t|| find( words[eng.size()].begin(), words[eng.size()].end(), eng ) == words[eng.size()].end() )\n\t\t\t\t\tsetResult(result,sort,eng,i-eng.size());\n\t\t\t\t\teng.clear();\n\t\t\t\t}\n\t\t\t\tif( words.find(1) == words.end() \n\t\t\t\t\t|| find( words[1].begin(), words[1].end(), cstr ) == words[1].end() )\n\t\t\t\t\tsetResult(result,sort,cstr,i);\n\n\t\t\t}else{\n\t\t\t\teng.append(cstr);\n\t\t\t}\n\t\t}\n\t\tif(!eng.empty()){\n\t\t\tif( words.find(eng.size()) == words.end() \n\t\t\t\t|| find( words[eng.size()].begin(), words[eng.size()].end(), eng ) == words[eng.size()].end() )\n\t\t\t\tsetResult(result,sort,eng,content.size()-eng.size());\n\t\t\t\teng.clear();\n\t\t}\n\n\t}\n\n\t_WVECTOR_ line;\n\twstring one_line = L\"\";\n\n\t\/\/\u6bb5\u53e5\n\tfor( int i=0; i&lt;content.size(); i++ ){\n\t\twstring str = content.substr( i, 1 );\n\t\tif( find(end_dot.begin(),end_dot.end(),str) == end_dot.end() ){\n\t\t\tone_line.append(str);\n\t\t}else{\n\t\t\tline.insert(line.begin(),one_line);\n\t\t\tone_line.clear();\n\t\t}\n\t\tif( i==content.size() - 1 &amp;&amp; !one_line.empty() ){\n\t\t\tline.insert(line.begin(),one_line);\n\t\t}\n\t}\n\t\n\tint content_size = content.size(), offset = content_size, sort = 0;\n\n\tfor(_WVECTOR_::iterator i=line.begin(); i != line.end(); i++){\n\t\toffset -= (*(i)).size();\n\t\tif( i != line.begin() ){\n\t\t\toffset -= 1;\t\/\/\u65ad\u53e5\u7b26\u53f7\u4f4d\u7f6e\n\t\t}\n\t\t\/\/\u5206\u8bcd\n\t\twstring str = *i;\t\t\t\/\/\u6bb5\u5185\u5bb9\n\t\twstring cstr;\t\t\t\t\/\/\u5206\u6bb5\u5185\u5bb9\n\t\tint begin = 0,pointer;\t\t\/\/\u6e38\u6807\uff0c\u4e00\u4e2a\u5168\u5c40\u6e38\u6807\uff0c\u548c\u4e00\u4e2a\u76f8\u5bf9\u6e38\u6807\n\t\tint k;\t\t\t\t\t\t\/\/\u8bcd\u5178\u952e\n\t\tint sublength = max_length;\t\/\/\u6bcf\u4e2a\u67e5\u8be2\u5b57\u7b26\u7684\u957f\u5ea6\n\t\t\n\t\tif( str.size() > max_length ){\n\t\t\tbegin = str.size() - max_length;\n\t\t}else{\n\t\t\tsublength = str.size();\n\t\t}\n\t\t\n\t\twhile (begin != -sublength)\n\t\t{\n\t\t\tpointer = 0;\n\t\t\twhile( pointer &lt; sublength ){\n\t\t\t\tif( begin >= 0 ){\n\t\t\t\t\tcstr = str.substr( begin, sublength-pointer );\n\t\t\t\t\tk = cstr.size();\n\t\t\t\t\t\n\t\t\t\t\tif( words.find(k) != words.end() &amp;&amp; find(words[k].begin(),words[k].end(),cstr)!=words[k].end() ){\n\t\t\t\t\t\tsetResult(result,sort,cstr,offset+begin);\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tpointer++;\n\t\t\t\tif( pointer &lt; sublength )\n\t\t\t\t\tbegin++;\n\t\t\t}\n\t\t\tbegin-=sublength;\n\t\t}\n\t}\n\treturn result;\n}\n\n\nvoid WordPart::setResult( map&lt;wstring,_WordPartResult> &amp;result, int &amp;sort, const wstring k, const int offset ){\n\tif( result.find(k) == result.end() ){\n\t\t_WordPartResultItem row;\n\t\tresult.insert( map&lt;wstring,_WordPartResult>::value_type(k,row) );\n\t\tresult[k].startOffset = offset;\n\t\tresult[k].sort = sort;\n\t\tsort++;\n\t}\n\tresult[k].count += 1;\n\tresult[k].endOffset = offset;\n} <\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,8],"tags":[17],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-10","category-8","tag-c"],"_links":{"self":[{"href":"https:\/\/joson.cc\/index.php?rest_route=\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/joson.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/joson.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/joson.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/joson.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=57"}],"version-history":[{"count":2,"href":"https:\/\/joson.cc\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":59,"href":"https:\/\/joson.cc\/index.php?rest_route=\/wp\/v2\/posts\/57\/revisions\/59"}],"wp:attachment":[{"href":"https:\/\/joson.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/joson.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/joson.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}