﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-yefeng-随笔分类-C/C++技术</title><link>http://www.cppblog.com/yefeng/category/12093.html</link><description>夜风'blog</description><language>zh-cn</language><lastBuildDate>Mon, 07 Dec 2009 12:45:06 GMT</lastBuildDate><pubDate>Mon, 07 Dec 2009 12:45:06 GMT</pubDate><ttl>60</ttl><item><title>Aho-Corasick算法实践</title><link>http://www.cppblog.com/yefeng/archive/2009/12/06/102671.html</link><dc:creator>夜风</dc:creator><author>夜风</author><pubDate>Sun, 06 Dec 2009 14:51:00 GMT</pubDate><guid>http://www.cppblog.com/yefeng/archive/2009/12/06/102671.html</guid><wfw:comment>http://www.cppblog.com/yefeng/comments/102671.html</wfw:comment><comments>http://www.cppblog.com/yefeng/archive/2009/12/06/102671.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/yefeng/comments/commentRss/102671.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yefeng/services/trackbacks/102671.html</trackback:ping><description><![CDATA[<meta http-equiv="Content-Type" content="text/html; charset=" utf-8="">
<meta name="ProgId" content="Word.Document">
<meta name="Generator" content="Microsoft Word 11">
<meta name="Originator" content="Microsoft Word 11">
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cyefeng%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml">
<link rel="Edit-Time-Data" href="file:///C:%5CDOCUME%7E1%5Cyefeng%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_editdata.mso">
<link rel="OLE-Object-Data" href="file:///C:%5CDOCUME%7E1%5Cyefeng%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_oledata.mso"><!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]--><!--[if gte mso 9]><xml>
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
</xml><![endif]--><!--[if gte mso 9]><xml>
</xml><![endif]--><style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:黑体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimHei;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@黑体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
h1
{mso-style-next:正文;
margin-top:17.0pt;
margin-right:0cm;
margin-bottom:16.5pt;
margin-left:0cm;
text-align:center;
line-height:240%;
mso-pagination:lines-together;
page-break-after:avoid;
mso-outline-level:1;
font-size:22.0pt;
font-family:"Times New Roman";
mso-font-kerning:22.0pt;}
h2
{mso-style-next:正文;
margin-top:13.0pt;
margin-right:0cm;
margin-bottom:13.0pt;
margin-left:21.0pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:-21.0pt;
line-height:173%;
mso-pagination:lines-together;
page-break-after:avoid;
mso-outline-level:2;
mso-list:l1 level1 lfo1;
tab-stops:list 21.0pt;
font-size:16.0pt;
font-family:Arial;
mso-fareast-font-family:黑体;
mso-bidi-font-family:"Times New Roman";
mso-font-kerning:1.0pt;}
h3
{mso-style-next:正文;
margin-top:13.0pt;
margin-right:0cm;
margin-bottom:13.0pt;
margin-left:21.0pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:-21.0pt;
line-height:173%;
mso-pagination:lines-together;
page-break-after:avoid;
mso-outline-level:3;
mso-list:l2 level1 lfo2;
tab-stops:list 21.0pt;
font-size:16.0pt;
font-family:"Times New Roman";
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:595.3pt 841.9pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:42.55pt;
mso-footer-margin:49.6pt;
mso-paper-source:0;
layout-grid:15.6pt;}
div.Section1
{page:Section1;}
/* List Definitions */
@list l0
{mso-list-id:59057800;
mso-list-type:hybrid;
mso-list-template-ids:954916874 -1935886898 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
{mso-level-tab-stop:60.0pt;
mso-level-number-position:left;
margin-left:60.0pt;
text-indent:-18.0pt;}
@list l1
{mso-list-id:1262569428;
mso-list-type:hybrid;
mso-list-template-ids:-1597851618 277625704 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l1:level1
{mso-level-number-format:chinese-counting-thousand;
mso-level-style-link:"标题 2";
mso-level-text:%1、;
mso-level-tab-stop:21.0pt;
mso-level-number-position:left;
margin-left:21.0pt;
text-indent:-21.0pt;}
@list l2
{mso-list-id:1873759819;
mso-list-type:hybrid;
mso-list-template-ids:-1017220182 1075860886 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l2:level1
{mso-level-style-link:"标题 3";
mso-level-tab-stop:21.0pt;
mso-level-number-position:left;
margin-left:21.0pt;
text-indent:-21.0pt;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->
</style><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
table.MsoTableGrid
{mso-style-name:网格型;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
border:solid windowtext 1.0pt;
mso-border-alt:solid windowtext .5pt;
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-border-insideh:.5pt solid windowtext;
mso-border-insidev:.5pt solid windowtext;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
<h1>Aho-Corasick算法实践</h1>
<strong><span style="font-size: 10pt;">摘要：</span></strong><span style="font-size: 12pt;"><span style="font-size: 10pt;">
<p>&nbsp;&nbsp;&nbsp; Aho-Corasick算法可以在文本串中识别一组关键字，所需时间和文本长度以及所有关键字的总长度成正比。该算法使用了一种称为&#8220;trie&#8221;的特殊形式的状态装换图。Trie是一个树形结构的状态装换图，从一个结点到它的各个子结点的边上有不同的标号。Trie的叶子结点表示识别到的关键字。</p>
<p>&nbsp;&nbsp;&nbsp; 在这里，将着重讨论算法的实现。算法包含两个部分，一是经典的KMP算法，二是KMP的扩展算法Aho-Corasick算法。前者实现单关键字的模式匹配，后者实现多关键字的匹配。(参考龙书词法分析部分内容)</p>
<p>&nbsp;&nbsp; 【源代码：<a style="color: #303dff;" href="http://www.cppblog.com/Files/yefeng/ACKMP.rar">http://www.cppblog.com/Files/yefeng/ACKMP.rar</a>(vc9.0下测试通过)
】</p>
</span></span>
<meta http-equiv="Content-Type" content="text/html; charset=" utf-8="">
<meta name="ProgId" content="Word.Document">
<meta name="Generator" content="Microsoft Word 11">
<meta name="Originator" content="Microsoft Word 11">
<link rel="File-List" href="file:///C:%5CDOCUME%7E1%5Cyefeng%5CLOCALS%7E1%5CTemp%5Cmsohtml1%5C01%5Cclip_filelist.xml"><!--[if gte mso 9]><xml>
Normal
0
7.8 磅
0
2
false
false
false
MicrosoftInternetExplorer4
</xml><![endif]--><!--[if gte mso 9]><xml>
</xml><![endif]--><style>
<!--
/* Font Definitions */
@font-face
{font-family:宋体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimSun;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:黑体;
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-alt:SimHei;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
@font-face
{font-family:"\@宋体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:3 135135232 16 0 262145 0;}
@font-face
{font-family:"\@黑体";
panose-1:2 1 6 0 3 1 1 1 1 1;
mso-font-charset:134;
mso-generic-font-family:auto;
mso-font-pitch:variable;
mso-font-signature:1 135135232 16 0 262144 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-parent:"";
margin:0cm;
margin-bottom:.0001pt;
text-align:justify;
text-justify:inter-ideograph;
mso-pagination:none;
font-size:10.5pt;
mso-bidi-font-size:12.0pt;
font-family:"Times New Roman";
mso-fareast-font-family:宋体;
mso-font-kerning:1.0pt;}
h2
{mso-style-next:正文;
margin-top:13.0pt;
margin-right:0cm;
margin-bottom:13.0pt;
margin-left:21.0pt;
text-align:justify;
text-justify:inter-ideograph;
text-indent:-21.0pt;
line-height:173%;
mso-pagination:lines-together;
page-break-after:avoid;
mso-outline-level:2;
mso-list:l0 level1 lfo1;
tab-stops:list 21.0pt;
font-size:16.0pt;
font-family:Arial;
mso-fareast-font-family:黑体;
mso-bidi-font-family:"Times New Roman";
mso-font-kerning:1.0pt;}
/* Page Definitions */
@page
{mso-page-border-surround-header:no;
mso-page-border-surround-footer:no;}
@page Section1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;
mso-header-margin:36.0pt;
mso-footer-margin:36.0pt;
mso-paper-source:0;}
div.Section1
{page:Section1;}
/* List Definitions */
@list l0
{mso-list-id:1262569428;
mso-list-type:hybrid;
mso-list-template-ids:-1597851618 277625704 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
{mso-level-number-format:chinese-counting-thousand;
mso-level-style-link:"标题 2";
mso-level-text:%1、;
mso-level-tab-stop:21.0pt;
mso-level-number-position:left;
margin-left:21.0pt;
text-indent:-21.0pt;}
ol
{margin-bottom:0cm;}
ul
{margin-bottom:0cm;}
-->
</style><!--[if gte mso 10]>
<style>
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.0pt;
font-family:"Times New Roman";
mso-ansi-language:#0400;
mso-fareast-language:#0400;
mso-bidi-language:#0400;}
</style>
<![endif]-->
<h2><!--[if !supportLists]-->一、<!--[endif]-->经典KMP算法</h2>
&nbsp;&nbsp;&nbsp; <span style="font-size: 10pt;">当初，初学KMP算法时，总是通过反复的举例去理解，没有一种好的表达方式，而龙书描述这个算法使用了trie树，也就是一个单链的状态转换图。如模式<!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->b0b1...bn-1<!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->，trie树如下：</span>
<p>&nbsp;&nbsp;&nbsp;&nbsp; <img style="width: 511px; height: 99px;" alt="" src="http://www.cppblog.com/images/cppblog_com/yefeng/clip_image002.jpg"><br><!--[endif]--></p>
<span style="font-size: 10pt;">
<p>&nbsp;&nbsp;&nbsp; 对模式串定义失效函数<!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->f:x-&gt;y,x,y in S<!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->，描述状态转移，<!--[if gte vml 1]>
<![endif]--><!--[if !vml]--><!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->f(s)表示在状态s处，当下一个字符不是bs<!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->时转向状态<!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->f(s)<!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->继续匹配。因此设置<!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->f(s)<!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->成为关键问题。</p>
<p><!--[if gte vml 1]>
<![endif]--><!--[if !vml]--><!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->&nbsp;&nbsp;&nbsp; f(s)的存在其实主要是为了消除回溯。细节就不再多说了，这里只从原理上简单说明。</p>
<p>&nbsp;&nbsp;&nbsp; 设模式串为W，用文法描述，U、V表示W的一部分,w表示一个字符：</p>
<p style="text-align: center;">&nbsp;&nbsp;&nbsp; W -&gt; UwV，</p>
<p>&nbsp;&nbsp;&nbsp; 当U识别完成后，进入状态s，识别w时，发现到来的字符不等于w，则需要转向状态<!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->f(s)<!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->，f(s)<!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->到哪里去找呢？</p>
<p style="text-align: left;">&nbsp;&nbsp;&nbsp; 那就要看U是什么样子了。不管什么情况，只要U非空串，总可以表示成：</p>
<p style="text-align: center;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; U -&gt; uXu，或 U -&gt; u，或U-&gt; uXx，(x != u)</p>
<p>&nbsp;&nbsp;&nbsp; 可以发现，前缀u是，如果后缀也是u，意味着主串中u已经被识别，如果还从模式串头匹配u无疑是多余的，所以<!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->f(s)<!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]-->应该是识别前缀u后进入的状态。然后再匹配下一个字符。而满足条件的u可能会有多个，所以总是选择最长的那个。伪代码如下：</p>
</span>
<p><!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->&nbsp;&nbsp;&nbsp; <img alt="" src="http://www.cppblog.com/images/cppblog_com/yefeng/clip_image002.gif" height="167" width="225"><br><!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]--></p>
<span style="font-size: 10pt;">
<p>&nbsp;&nbsp;&nbsp; 到此为止，应该算是可以结束KMP了，但实际情况下还可以对f函数进行优化。很多书本上描述的next数组就可以从f函数推导过来。</p>
<p>&nbsp;&nbsp;&nbsp; 其实也显然，设状态s接收字符w，当与输入字符c不等于c时，转向状态t，倘若t状态也只接收字符w，显然再次比较w与c是多余的，之后必然再次转向状态f(t)。在运行的时候，这些状态转换时没有意义的，可以在构造f之后，直接将f(s)设置为f(t)提高运行效率(不过此时f函数的意义已经不同了)。f优化如下：</p>
</span>
<p><!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->&nbsp;&nbsp;&nbsp; <img alt="" src="http://www.cppblog.com/images/cppblog_com/yefeng/a.gif" height="167" width="225"><br><!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]--></p>
<h2><!--[if !supportLists]-->二、<!--[endif]-->多关键字匹配与Aho-Corasick算法</h2>
<p>&nbsp;&nbsp;&nbsp; <span style="font-size: 10pt;">Aho和Corasick对KMP算法进行了推广，使它可以在一个文本串识别一个关键字集合中的任何关键字。在这种情况下，trie是一棵真正的树，从其根结点开始就会出现分支。如果一个字符串是某个关键字的前缀，那么在trie中就又一个和该字符串对应的状态。如关键字集合{he,she,his,hers}，trie树如下：</span></p>
<p><!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->&nbsp;&nbsp;&nbsp; <img style="width: 444px; height: 248px;" src="http://www.cppblog.com/images/cppblog_com/yefeng/bb.jpg" border="0"><br><!--[endif]--></p>
<p>&nbsp;&nbsp;&nbsp; </p>
<span style="font-size: 10pt;">
<p>&nbsp;&nbsp;&nbsp; 类似的，仍然构造类似KMP算法中那样的实效函数。对于上面的例子，失效函数如下：</p>
<table class="MsoTableGrid" style="margin-left: 21pt; border-collapse: collapse;" border="1" cellpadding="0" cellspacing="0">
    <tbody>
        <tr>
            <td style="padding: 0cm 5.4pt; width: 38.7pt;" valign="top" width="52">
            <p>s</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.7pt;" valign="top" width="52">
            <p>0</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.7pt;" valign="top" width="52">
            <p>1</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>2</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>3</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>4</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>5</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>6</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>7</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>8</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>9</p>
            </td>
        </tr>
        <tr>
            <td style="padding: 0cm 5.4pt; width: 38.7pt;" valign="top" width="52">
            <p>f(s)</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.7pt;" valign="top" width="52">
            <p>-1</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.7pt;" valign="top" width="52">
            <p>0</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>0</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>0</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>1</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>2</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>0</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>3</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>0</p>
            </td>
            <td style="padding: 0cm 5.4pt; width: 38.75pt;" valign="top" width="52">
            <p>3</p>
            </td>
        </tr>
    </tbody>
</table>
</span><br>
<h3><!--[if !supportLists]-->&nbsp; 1.<!--[endif]-->构造失效函数</h3>
<p>&nbsp;&nbsp;&nbsp; <span style="font-size: 10pt;">类似KMP算法，同样采用实效实效函数推进的方法，假设当前状态为s，s的一个孩子结点的根结点根节点t状态，如果当前的失效函数已知为f(s)，则显然地，f(t)必定是f(s)的孩子结点状态，所要做的就是在状态f(s)处寻找接受字符同s-&gt;t下一个状态，如果能找到，那就是f(t)，否则说明到s处匹配串的前缀长度太长，需缩减，所以需要找到更短的后缀，于是就到f(s)处继续，如果仍然找不到，则转到f(f(s))处，形成状态的递归转移。构造中需要遍历之前结点的所有孩子，所以需采用广度优先遍历，伪代码如下：</span></p>
<p><!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->&nbsp;&nbsp;&nbsp; <img alt="" src="http://www.cppblog.com/images/cppblog_com/yefeng/cc.gif" height="295" width="180"><br><!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]--></p>
<p>&nbsp;&nbsp;&nbsp; <span style="font-size: 10pt;">具体的构造如下：</span></p>
<p><!--[if gte vml 1]>
<![endif]--><!--[if !vml]-->&nbsp;&nbsp;&nbsp; <img style="width: 194px; height: 289px;" alt="" src="http://www.cppblog.com/images/cppblog_com/yefeng/dd.gif"><br><!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]--></p>
<h3><!--[if !supportLists]-->&nbsp; 2.构造Trie树</h3>
<span style="font-size: 10pt;">
<p>&nbsp;&nbsp;&nbsp; 具体实现当然需要用到树形结构了，显然采用静态链表应该是最适合的，因为树构造完就不需要改变，而且当模式串比较多的时候可以减少内存碎片。</p>
<p>&nbsp; &nbsp; 每一个结点有5个域：接受字符，下一个兄弟结点，第一个孩子结点，失效函数值，结点状态。</p>
<p>但是有一种特殊情况，如上面的第二个图，在进行匹配时，hers是永远不会被匹配，因为he总是先于hers被匹配。这里就不考虑在内点状态结束，这个问题暂时无法解决。于是可以做个特殊处理，只使用4个域，因为此时匹配成功后状态就到了叶子结点，叶子结点不存在孩子域，这个域被浪费了，这里就可以借用一下，比如此域值为x，当x&lt;0时，使用x xor 0x80000000表示识别到的模式串编号。</p>
<p>&nbsp;&nbsp;&nbsp; 另一个棘手的问题是结点个数问题，这个数组到底多大？如何确定？</p>
<p>&nbsp;&nbsp;&nbsp; 可以使用分值算法计算，先把模式串按字典顺序排好序，设想n个排好序的模式串第i位排在一起，相同字符的组成一组，如AiBi&#8230;Xi，再把每组下一个字符，也就是第i+1位排在一起，相同字符的组成一组，如A&#8217;iB&#8217;I&#8230;X&#8217;i，以此递归运算。伪代码如下：</p>
</span>
<p>&nbsp;&nbsp;&nbsp;&nbsp; <img style="width: 334px; height: 288px;" alt="" src="http://www.cppblog.com/images/cppblog_com/yefeng/xx.gif"><br><!--[endif]--><!--[if gte mso 9]><xml>
</xml><![endif]--></p>
<h3><!--[if !supportLists]-->&nbsp; 3.缺点</h3>
<span style="font-size: 10pt;">
<p>&nbsp;&nbsp;&nbsp; 水平有限，程序缺点很多，很多问题都没有解决。</p>
<p><!--[if !supportLists]-->&nbsp;&nbsp;&nbsp; 1.<!--[endif]-->如果存在两个模式串，一个是另一个的子串，那么后者将无法被匹配。</p>
<p><!--[if !supportLists]-->&nbsp;&nbsp;&nbsp; 2.<!--[endif]-->无法处理动态决定大小写敏感性</p>
<p><!--[if !supportLists]-->&nbsp;&nbsp;&nbsp; 3.<!--[endif]-->不够完整，只能向后匹配</p>
</span> <img src ="http://www.cppblog.com/yefeng/aggbug/102671.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yefeng/" target="_blank">夜风</a> 2009-12-06 22:51 <a href="http://www.cppblog.com/yefeng/archive/2009/12/06/102671.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>虚拟键盘(软键盘)设计要点</title><link>http://www.cppblog.com/yefeng/archive/2009/10/18/98905.html</link><dc:creator>夜风</dc:creator><author>夜风</author><pubDate>Sun, 18 Oct 2009 15:19:00 GMT</pubDate><guid>http://www.cppblog.com/yefeng/archive/2009/10/18/98905.html</guid><wfw:comment>http://www.cppblog.com/yefeng/comments/98905.html</wfw:comment><comments>http://www.cppblog.com/yefeng/archive/2009/10/18/98905.html#Feedback</comments><slash:comments>6</slash:comments><wfw:commentRss>http://www.cppblog.com/yefeng/comments/commentRss/98905.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/yefeng/services/trackbacks/98905.html</trackback:ping><description><![CDATA[<span style="FONT-SIZE: 18pt"><span style="FONT-SIZE: 10pt"><span style="FONT-SIZE: 10pt">&nbsp;&nbsp;&nbsp; 前些天花了很多时间写这样一个软键盘，效果是显示一个与键盘外观相似的视图，通过鼠标单击像活动窗口发送虚拟的键盘消息。目标是实现像windows自带的软键盘osk相似。<br>&nbsp;&nbsp;&nbsp; 看似很简单的工作，设计中却遇到了很多困难。<br>&nbsp;&nbsp;&nbsp; 困难一：键盘按键分类<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 键盘按键有很多种分类方法。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 第一种：按显示分类。按住shift键，字母键、符号键显示上面的字符；按下caps lock键，字母键切换为大写字母。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 第二种：按功能分类。大体有可显示字符类、控制类。控制类包括shift，ctrl等。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为了解决可变的显示问题，采用了一个自我感觉非常好的解决方案：字符集、键集相互独立。如此一来，只要总体按照功能分类，通过特定功能的按键控制有效字符集即可，也就是说，对普通按键来说，它只负责到指定的字符集中去取对应序号的字符即可。<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="FONT-SIZE: 10pt; COLOR: #008000">//</span><span style="COLOR: #008000">LabelSet.h</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">#pragma&nbsp;once<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">字母标签集合</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;LabelSet<br>{<br></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;LabelSet(LPCSTR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;_pTable,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_n);<br>&nbsp;&nbsp;&nbsp;&nbsp;LPCSTR&nbsp;getLabel(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_id)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">LabelSet();<br><br></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;LabelSet(){}<br><br></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;LPCSTR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pTable;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n;<br>};<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">相当于单刀双掷开关组</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">class</span><span style="COLOR: #000000">&nbsp;LabelSetEx<br>{<br></span><span style="COLOR: #0000ff">protected</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;Switch<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LabelSet</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;s[</span><span style="COLOR: #000000">2</span><span style="COLOR: #000000">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;at;<br>&nbsp;&nbsp;&nbsp;&nbsp;};<br><br></span><span style="COLOR: #0000ff">public</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;LabelSetEx(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_n);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;addSets(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;id,LPCSTR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;s1,LPCSTR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;s2,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;at&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;LPCSTR&nbsp;getLable(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;id,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;off)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;turn(</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;id);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">LabelSetEx();<br><br></span><span style="COLOR: #0000ff">private</span><span style="COLOR: #000000">:<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">开关组总个数</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;Switch</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;pGroup;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">开关组</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">};<br><br></span><span style="COLOR: #008000">//</span><br><span style="COLOR: #008000">//</span><span style="COLOR: #008000">LabelSet.cpp</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">#include&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">StdAfx.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000">LabelSet.h</span><span style="COLOR: #000000">"</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">algorithm</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br>#include&nbsp;</span><span style="COLOR: #000000">&lt;</span><span style="COLOR: #000000">cassert</span><span style="COLOR: #000000">&gt;</span><span style="COLOR: #000000"><br><br></span><span style="COLOR: #0000ff">using</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">namespace</span><span style="COLOR: #000000">&nbsp;std;<br><br>LabelSet::LabelSet(&nbsp;LPCSTR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;_pTable,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_n&nbsp;)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;_n;<br>&nbsp;&nbsp;&nbsp;&nbsp;pTable&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;LPCSTR[n];<br>&nbsp;&nbsp;&nbsp;&nbsp;copy(_pTable,_pTable&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;_n,pTable);<br>}<br><br>LPCSTR&nbsp;LabelSet::getLabel(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_id&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;pTable[_id];<br>}<br><br>LabelSet::</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">LabelSet()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;[]&nbsp;pTable;<br>}<br><br>LabelSetEx::LabelSetEx(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;_n&nbsp;)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;n&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;_n;<br>&nbsp;&nbsp;&nbsp;&nbsp;pGroup&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;Switch[n];<br>&nbsp;&nbsp;&nbsp;&nbsp;memset(pGroup,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,n&nbsp;</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">sizeof</span><span style="COLOR: #000000">(pGroup[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]));<br>}<br><br>LabelSetEx::</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">LabelSetEx()<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">while</span><span style="COLOR: #000000">(n</span><span style="COLOR: #000000">--</span><span style="COLOR: #000000">)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(pGroup[n].s[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;pGroup[n].s[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">])<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pGroup[n].s[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pGroup[n].s[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pGroup[n].s[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;[]&nbsp;pGroup;<br>}<br><br></span><span style="COLOR: #0000ff">bool</span><span style="COLOR: #000000">&nbsp;LabelSetEx::addSets(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;id,LPCSTR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;s1,LPCSTR</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;s2,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;n,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;at&nbsp;</span><span style="COLOR: #008000">/*</span><span style="COLOR: #008000">=&nbsp;0</span><span style="COLOR: #008000">*/</span><span style="COLOR: #000000">&nbsp;)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;assert((at&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(pGroup[id].s[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;NULL)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">false</span><span style="COLOR: #000000">;<br>&nbsp;&nbsp;&nbsp;&nbsp;LabelSet</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;LabelSet(s1,n);<br>&nbsp;&nbsp;&nbsp;&nbsp;pGroup[id].s[</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;p;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(s1&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;s2)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pGroup[id].s[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;p;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">else</span><span style="COLOR: #000000"><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pGroup[id].s[</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">]&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">new</span><span style="COLOR: #000000">&nbsp;LabelSet(s2,n);<br>&nbsp;&nbsp;&nbsp;&nbsp;pGroup[id].at&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;at;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">true</span><span style="COLOR: #000000">;<br>}<br><br>LPCSTR&nbsp;LabelSetEx::getLable(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;id,</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;off&nbsp;)&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000"><br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;Switch</span><span style="COLOR: #000000">*</span><span style="COLOR: #000000">&nbsp;p&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;pGroup&nbsp;</span><span style="COLOR: #000000">+</span><span style="COLOR: #000000">&nbsp;id;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">return</span><span style="COLOR: #000000">&nbsp;p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">s[p</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">at]</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">getLabel(off);<br>}<br><br></span><span style="COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;LabelSetEx::turn(&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;id&nbsp;)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;assert((pGroup</span><span style="COLOR: #000000">-&gt;</span><span style="COLOR: #000000">at&nbsp;</span><span style="COLOR: #000000">&amp;</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">~</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">)&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;pGroup[id].at&nbsp;</span><span style="COLOR: #000000">^=</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #000000">1</span><span style="COLOR: #000000">;<br>}</span></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 以上取开关的索引id是指字符集的分类id，在config.h文件下定义了这样的id<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="FONT-SIZE: 10pt; COLOR: #000000">#pragma&nbsp;once<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">分类id的定义</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;LABEL_SET_ALPHA&nbsp;&nbsp;0</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;LABEL_SET_SYMBOL&nbsp;1</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;LABEL_SET_NUMPAD&nbsp;2</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;LABEL_SET_MAIN&nbsp;&nbsp;&nbsp;3</span><span style="COLOR: #000000"><br></span><span style="COLOR: #0000ff">#define</span><span style="COLOR: #000000">&nbsp;LABEL_SET_HELP&nbsp;&nbsp;&nbsp;4</span><span style="COLOR: #000000"><br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">字母串表</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;LPCSTR&nbsp;AlphaTable1[];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">小写</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;LPCSTR&nbsp;AlphaTable2[];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">大写</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;AlphaTableSize;<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">符号串表</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;LPCSTR&nbsp;SymbolTable1[];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">下</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;LPCSTR&nbsp;SymbolTable2[];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">上</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;SymbolTableSize;<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">小键盘数字表</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;LPCSTR&nbsp;NumPadTable1[];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">数字</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;LPCSTR&nbsp;NumPadTable2[];&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">光标控制</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;NumPadTableSize;<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">主键盘单显</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;LPCSTR&nbsp;MainTable[];<br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;MainTableSize;<br><br></span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">辅助键盘单显</span><span style="COLOR: #008000"><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;LPCSTR&nbsp;HelpTable[];<br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;HelpTableSize;<br><br></span><span style="COLOR: #0000ff">struct</span><span style="COLOR: #000000">&nbsp;KeyConfig<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000">&nbsp;id;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">分类id</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">short</span><span style="COLOR: #000000">&nbsp;offset;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">类内偏移</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;RECT&nbsp;rt;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">位置</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">&nbsp;&nbsp;&nbsp;&nbsp;BYTE&nbsp;vk;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #008000">//</span><span style="COLOR: #008000">虚拟码</span><span style="COLOR: #008000"><br></span><span style="COLOR: #000000">};<br><br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;KeyConfig&nbsp;kcs[];<br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">int</span><span style="COLOR: #000000">&nbsp;kcSize;<br></span><span style="COLOR: #0000ff">extern</span><span style="COLOR: #000000">&nbsp;</span><span style="COLOR: #0000ff">const</span><span style="COLOR: #000000">&nbsp;SIZE&nbsp;kbSize;</span></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 第一次这样写代码，写完发现这样极大地提高了灵活性，只要在配置文件config.cpp中修改，就可以产生很多种不同的界面（虽然仍然是代码级别的，毕竟迈出了第一步，今后还会尝试改成xml配置）。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 言归正传，这样的设计分离了按键与显示，可配置能力大大加强。但仍然存在第二个大问题。<br>&nbsp;&nbsp;&nbsp; 问题二：输入焦点的确定<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 方案一：现在只要在网上搜索&#8220;虚拟键盘&#8221;，能够搜到一大溜的源代码，但只可惜全是同一份拷贝，而且存在一点小错误。他的解决方案是：利用 PreTranslateMessage，在底层调用它之前，前台窗口仍然没有改变，此时是获得前一个前台窗口的好时机，获得后保存，并将使用 AttachThreadInput将当前线程绑定活动窗口的消息队列，然后在单击虚拟键盘时使用SetFocus将保存的窗口设为焦点（源代码中同时使用了SetForgroundWindow和SetFocus，这是失效的原因），然后发送虚拟按键。<br><img height=291 alt="" src="http://www.cppblog.com/images/cppblog_com/yefeng/a.JPG" width=540 border=0><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 方案二：其实有更简便的方法。设置主窗口属性为WM_ES_NOACTIVATE，这样窗口就不会成为前台窗口，不管如何发送键盘消息，拥有焦点的窗口总会收到。但此时仍然存在问题。当移动窗口时，效果不大顺畅，而且没办法响应菜单命令，那是因为该窗口始终不是前台窗口造成的。解决方法就是在单击标题栏时，成为前台窗口，释放是归还前台。<br><img height=279 alt="" src="http://www.cppblog.com/images/cppblog_com/yefeng/c.JPG" width=414 border=0><br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="FONT-SIZE: 10pt; COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;CMainFrame::OnNcLButtonDown(UINT&nbsp;nHitTest,&nbsp;CPoint&nbsp;point)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(m_hForground&nbsp;</span><span style="COLOR: #000000">==</span><span style="COLOR: #000000">&nbsp;NULL)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_hForground&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;::GetForegroundWindow();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ModifyStyleEx(WS_EX_NOACTIVATE,</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SetForegroundWindow();<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;CFrameWnd::OnNcLButtonDown(nHitTest,&nbsp;point);<br>}</span></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 但是，如果想当然归还前台使用WM_NCLBUTTONUP消息的话，就要让你失望了，windows似乎有意跟我们开玩笑，必须单击两次才能响应这个消息。没办法，于是尝试WM_NCMOUSELEAVE，但效果也不好，最终尝试WM_NCMOUSEMOVE，很好，这次终于成功了。<br>
<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="FONT-SIZE: 10pt; COLOR: #0000ff">void</span><span style="COLOR: #000000">&nbsp;CMainFrame::OnNcMouseMove(UINT&nbsp;nHitTest,&nbsp;CPoint&nbsp;point)<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="COLOR: #0000ff">if</span><span style="COLOR: #000000">(m_hForground&nbsp;</span><span style="COLOR: #000000">!=</span><span style="COLOR: #000000">&nbsp;NULL)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;::SetForegroundWindow(m_hForground);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ModifyStyleEx(</span><span style="COLOR: #000000">0</span><span style="COLOR: #000000">,WS_EX_NOACTIVATE);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m_hForground&nbsp;</span><span style="COLOR: #000000">=</span><span style="COLOR: #000000">&nbsp;NULL;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>&nbsp;&nbsp;&nbsp;&nbsp;CFrameWnd::OnNcMouseMove(nHitTest,&nbsp;point);<br>}</span></div>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 问题到此为止，现在说说一点小小的发现。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 原本以为一般的按键就两种状态，通过down、up改变，如果用方波描述，down就是下降沿触发，up是上升沿触发。也曾了解，像shift这样的按键会很复杂，存在多个状态。后来测试发现，shift并非一个特例，所有的按键都有4个状态，通过down、up改变状态。只是不同按键对状态的关注点不同。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 可以做这样一个测试，用GetKeyboardState得到各个虚拟码对应的按键状态。最高位为1时表示键被按下，最高位为1时，如果是lock键则表示被锁住，对于其他键，各有各的作用。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 比如一个键，用2位的二进制数表示这些状态，设初始状态为10，经过down后，变为01，经过up后，变为11,再经过down后，变为00，再经过up后，变为10，如此四个状态经过down、up实现了周期性的状态装换。大体符合这样的规律：<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 10-(down xor 11)-&gt;01-&gt;(up xor 10)-&gt;11-(down xor 11)-&gt;00(up xor 10)-&gt;10。<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这样，如果虚拟得比较彻底，在虚拟键盘内部可以轻易地实现状态的记忆，并且可以获得足够的信息。对于显示、控制都非常方便。<br><br>&nbsp;&nbsp;&nbsp; 这只是第一个版本，还有很多问题需要解决。<br>&nbsp;&nbsp;&nbsp; 待解决问题一：xml配置动态配置键盘，及动态更换显示效果。<br>&nbsp;&nbsp;&nbsp; 待解决问题二：同步物理键盘。<br>&nbsp;&nbsp;&nbsp; 待解决问题三：更深层次，防止键盘消息被hook，初步认识，似乎可以使用剪贴板。<br>&nbsp;&nbsp; 【源代码1.2版本：<a href="http://www.cppblog.com/Files/yefeng/VirtualKeyboard1.2.rar">http://www.cppblog.com/Files/yefeng/VirtualKeyboard1.2.rar</a>】</span></span></span> 
<img src ="http://www.cppblog.com/yefeng/aggbug/98905.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/yefeng/" target="_blank">夜风</a> 2009-10-18 23:19 <a href="http://www.cppblog.com/yefeng/archive/2009/10/18/98905.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>