• <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, 評論 - 6, 引用 - 0
            數據加載中……

            poj 3691 AC自動機+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 閱讀(243) 評論(0)  編輯 收藏 引用

            国产成人精品久久免费动漫| 久久无码高潮喷水| 久久国产乱子精品免费女| 日韩亚洲欧美久久久www综合网| 久久r热这里有精品视频| 欧美午夜A∨大片久久| 无码精品久久久天天影视| 99久久免费只有精品国产| 久久天天躁狠狠躁夜夜不卡 | 久久综合丝袜日本网| 18岁日韩内射颜射午夜久久成人| 婷婷久久综合九色综合绿巨人| 久久人人爽人人爽人人片AV不| 国产成人精品久久| 午夜天堂精品久久久久| 国产综合免费精品久久久| 久久影院综合精品| 久久这里的只有是精品23| 四虎国产精品免费久久久| 久久夜色精品国产欧美乱| 午夜精品久久久久久影视777| 国产成人精品久久二区二区| 久久综合九色综合网站| 久久午夜综合久久| 99久久综合国产精品二区| 东京热TOKYO综合久久精品| 一本一本久久A久久综合精品| 久久久久国产日韩精品网站| 99久久综合国产精品二区| 精品综合久久久久久97超人| 久久久久久久亚洲Av无码| 久久精品国产乱子伦| 久久久久久久久久久久久久| 欧美色综合久久久久久| 欧美一级久久久久久久大片| 久久综合九色欧美综合狠狠| 久久99精品国产麻豆不卡| 中文字幕亚洲综合久久2| 精品一久久香蕉国产线看播放| 99久久99久久精品国产| 久久AⅤ人妻少妇嫩草影院|