学习心得(code)

superlong@CoreCoder

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  74 Posts :: 0 Stories :: 5 Comments :: 0 Trackbacks

公告

文字可能放在http://blog.csdn.net/superlong100,此处存放代码

常用链接

留言簿(4)

我参与的团队

搜索

  •  

最新随笔

最新评论

  • 1. re: Poj 1279
  • 对于一个凹多边形用叉积计算面积 后能根据结果的正负来判断给的点集的时针方向?
  • --bsshanghai
  • 2. re: Poj 3691
  • 你写的这个get_fail() 好像并是真正的get_fail,也是说fail指向的串并不是当前结点的子串。为什么要这样弄呢?
  • --acmer1183
  • 3. re: HDU2295[未登录]
  • 这个是IDA* 也就是迭代加深@ylfdrib
  • --superlong
  • 4. re: HDU2295
  • 评论内容较长,点击标题查看
  • --ylfdrib
  • 5. re: HOJ 11482
  • 呵呵..把代码发在这里很不错..以后我也试试...百度的编辑器太烂了....
  • --csuft1

阅读排行榜

评论排行榜

#include <iostream>
#include 
<string.h>
using namespace std;

struct tree
{
    tree 
*next[128], *fail;
    
int state, flag;
};

tree sta[
1005];
int  indexx;

tree 
*root, *p;

inline 
void newn()
{
    sta[indexx].fail 
= NULL;
    sta[indexx].flag 
= 0;
    sta[indexx].state 
= indexx;
    
for(int i = 0; i < 128; i ++) sta[indexx].next[i] = 0;
}

void init()
{
    indexx 
= 0;
    newn();
    root 
= &sta[indexx ++];
}

void insert(char ch[])
{
    
int id, i = 0;
    p 
= root;
    
while(ch[i])
    {
        id 
= ch[i];
        
if(p->next[id] == 0)
        {
            newn();
            p
->next[id] = &sta[indexx ++];
        }
        p 
= p->next[id];
        i 
++;
    }
    p
->flag = 1;
}

tree 
*que[100001];

bool search(tree *q)
{
    
while(q != root)
    {
        
if(q->flag) return true;
        q 
= q->fail;
    }
    
return false;
}

void get_fail()
{
    
int close = -1, open = 0, i, pass;
    p 
= root; p->fail = root;
    que[
0= root;
    
while(close < open)
    {
        p 
= que[++close];
        tree 
*= p;
        pass 
= 0;
        
if(q->flag == 0) pass = search(q);
        
if(pass) q->flag = 1;
        
for(i = 0; i < 128; i ++)
        {
            
if(p->next[i] == 0)
            {
                
if(p == root) p->next[i] = root;
                
else          p->next[i] = p->fail->next[i];
            }
            
else
            {
                
if(p == root) p->next[i]->fail = root;
                
else          p->next[i]->fail = p->fail->next[i];
                que[
++open] = p->next[i];
            }
        }
    }
}

char str[85];

int greedy()
{
    gets(str);
    
int L = strlen(str), state, ans = 0;
    state 
= 0;
    
for(int i = 0; i < L; i ++)
    {
        state 
= sta[state].next[str[i]]->state;
        
if(sta[state].flag)
        {
            state 
= 0;
            ans 
++;
        }
    }
    
return ans;
}

int n, m;

int main()
{
    
while(scanf("%d%d"&n, &m),n+m)
    {
        init();
        
char dg[16];    
        gets(dg);
        
for(int i = 0; i < n; i ++)
        {
            gets(dg);
            insert(dg);
        }
        get_fail();
        
int ans = 0;
        
for(int i = 0; i < m; i ++) ans += greedy();
        printf(
"%d\n", ans);
        
    }
}

posted on 2009-09-09 13:30 superlong 阅读(229) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理