• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Drolca

            Apologize To Drolca
            隨筆 - 28, 文章 - 1, 評(píng)論 - 6, 引用 - 0
            數(shù)據(jù)加載中……

            poj 3691 AC自動(dòng)機(jī)+DP

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

            const int MAXN=20*55;
            const int MAXL=1005;
            const int K=4;// kind of child
            int dp[MAXL][MAXN];

            struct Node
            {
                Node 
            *next[K], *fail;
                
            int flag, id; // use for T[j].next[k]->id
                void Init(int index)
                
            {
                    id
            =index;
                    flag
            =0;
                    fail
            =NULL;
                    
            for(int i=0; i<K; i++)next[i]=NULL;
                }

            }
            * Q[MAXN/2], *root, T[MAXN];// Q for queue, root&T for tree;

            int index=0;
            Node 
            * newNode()
            {
                T[index].Init(index);
                
            return &T[index++];
            }


            int tokind(char k)
            {
                
            if(k=='A')return 0
                
            else if(k=='C')return 1;
                
            else if(k=='G')return 2
                
            else return 3;
            }


            void insert(char *str)
            {    
                
            if(root==NULL)
                    root
            =newNode();

                Node 
            *now=root;
                
            for(int i=0; str[i]; i++)
                
            {
                    
            int kind=tokind(str[i]);
                    
            if(now->next[kind]==NULL)
                        now
            ->next[kind]=newNode();
                    now
            =now->next[kind];
                }

                now
            ->flag=1;
            }


            int search(Node *p)
            {
                
            while(p!=root)
                
            {
                    p
            =p->fail;
                    
            if(p->flag)return 1;
                }

                
            return 0;
            }


            void buildAC()
            {
                
            int head=0, tail=0;
                root
            ->fail=NULL;

                Q[tail
            ++]=root;
                
            while(head<tail)
                
            {
                    Node 
            *now=Q[head++];
                    
            if(now->flag==0)now->flag=search(now);
                    
            for(int i=0; i<K; i++)
                    
            {
                        
            if(now->next[i]!=NULL)
                        
            {
                            
            if(now==root)
                                now
            ->next[i]->fail=root;
                            
            else
                                now
            ->next[i]->fail=now->fail->next[i];
                            Q[tail
            ++]=now->next[i];
                        }

                        
            else
                        
            {
                            
            if(now==root)
                                now
            ->next[i]=root;
                            
            else
                                now
            ->next[i]=now->fail->next[i];
                        }

                    }

                }

            }


            void update(int &a, int b){if(a==-1||b<a)a=b;}

            int slove(char *str)
            {
                memset(dp, 
            -1sizeof(dp));
                dp[
            0][0]=0;
                Node 
            *p=root;

                
            for(int i=0; str[i]; i++)
                
            {
                    
            for(int j=0; j<index; j++)
                    

                        
            if(dp[i][j]!=-1&&T[j].flag==0)
                        
            {
                            
            int kind=tokind(str[i]);
                            
            for(int k=0; k<K; k++)// need to match (k, kind)
                            {                
                                Node 
            *chd=T[j].next[k];
                                
            if(chd->flag==0)
                                    update(dp[i
            +1][chd->id],dp[i][j]+(k!=kind));    
                            }

                        }

                    }

                }

                
                
            int len=strlen(str);
                
            int res=-1;
                
            for(int ii=0; ii<index; ii++)
                    
            if(dp[len][ii]!=-1)update(res, dp[len][ii]);
                
            return res;
            }


            int main()
            {
                freopen (
            "in.txt""r", stdin);
                
            int N, Tcase=0;
                
            while(scanf("%d"&N)!=EOF&&N)
                
            {
                    root
            =NULL;
                    index
            =0;
                    
            for(int i=0; i<N; i++)
                    
            {
                        
            char s[25];
                        scanf(
            "%s", s);
                        insert(s);
                    }
                    
                    buildAC();
                    
            char ss[1005];
                    scanf(
            "%s", ss);    
                    printf(
            "Case %d: %d\n"++Tcase, slove(ss));        
                }

                
            return 0;
            }

            posted on 2012-04-13 19:28 Drolca 閱讀(252) 評(píng)論(0)  編輯 收藏 引用


            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            日本亚洲色大成网站WWW久久| 一本久道久久综合狠狠爱| 国产精品热久久毛片| 日本精品久久久久影院日本| 久久青青草原亚洲av无码app| 久久91精品久久91综合| 久久综合九色欧美综合狠狠| 亚洲综合精品香蕉久久网| 色综合久久中文综合网| 久久亚洲AV成人无码软件| 久久九九有精品国产23百花影院| 性做久久久久久久久| 久久国产一区二区| 亚洲国产欧洲综合997久久| 久久久精品日本一区二区三区| 国产色综合久久无码有码| 国产精品久久久天天影视香蕉 | 综合人妻久久一区二区精品| 色噜噜狠狠先锋影音久久| 久久夜色精品国产欧美乱| 久久精品国产欧美日韩99热| 精品久久久久久久久久中文字幕 | 999久久久国产精品| 亚洲国产另类久久久精品小说| 久久精品国产99国产精品| 久久久久国产一级毛片高清版| 日韩人妻无码精品久久久不卡| 亚洲美日韩Av中文字幕无码久久久妻妇 | 久久九九兔免费精品6| 热久久国产欧美一区二区精品 | 国产精品久久久久久久久鸭| 亚洲精品乱码久久久久久蜜桃| 国产精品成人99久久久久| 久久综合综合久久97色| 久久精品亚洲日本波多野结衣 | 国产精品免费久久| 亚洲国产精品人久久| 久久久久久综合一区中文字幕| 久久精品国产亚洲av麻豆小说| 日本五月天婷久久网站| 思思久久99热只有频精品66|