poj 3691

 

注意当a是b的字串的时候.. 要

#include <iostream>
#include <cstring>
#include <stdio.h>
#include <algorithm>

using namespace std;


const int NODE = 1010;
const int CH = 4;
const int inf = 0xffffff;
int chd[NODE][CH] , cnt;//结点个数
int word[NODE];//关键数组,记录每个单词尾结点的信息,每道题目都不一样
int fail[NODE];//传说中的失败指针
int Que[NODE];//辅助队列
int sw[150];//string swap每个字符对应的Index,方便模板化

void Ins(char *a, int val) {
 int p = 0;
 for(; *a ; a ++) {
  int c = sw[*a];
  if(!chd[p][c]) {
   //CC(chd[cnt] , 0);
   memset( chd[cnt], 0, sizeof( chd[cnt] ));
   word[cnt] = 0;
   chd[p][c] = cnt ++;
  }
  p = chd[p][c];
 }
 word[p] = val;
}

void AC() {
 int *s = Que , *e = Que;
 for ( int i = 0; i < CH; i++ )
    if(chd[0][i]) {
  fail[chd[0][i]] = 0;
  *e++ = chd[0][i];
 }
 while(s != e) {
  int p = *s++;
  for ( int i = 0 ; i < CH; i ++ ) {
   if( chd[p][i] ) {
    int v = chd[p][i];
    *e++ = v;
    fail[v] = chd[fail[p]][i];
    //word[v] += word[fail[v]];
    //对word[v]按word[fail[v]]里的内容进行处理
   } else {
    chd[p][i] = chd[fail[p]][i];
   }
  }
 }
}

void init ()
{
    fail[0] = 0;
 //for( int i = 0; i < CH; i++ ) sw[i] = i;
 sw['A'] = 0;
 sw['G'] = 1;
 sw['C'] = 2;
 sw['T'] = 3;
//下面两句每次都必须初始化
 //CC(chd[0],0);
 memset( chd[0], 0,sizeof( chd[0] ));
 memset ( word , 0 ,sizeof ( word ));
 cnt = 1;
}

//AC()函数处理后 chd[p][i] 就是在p结点进行i转移到达的结点


int dp[1005][1005];
int loc[1005][1005];
char str[55][55];
char p[1004];
int main()
{
    //freopen ( "data1.txt" , "r" , stdin);
    int n ;
    int Case = 0;
    while ( cin >> n , n )
    {
        Case ++;
        init();
        for ( int i = 0 ; i < n; i ++ )
        {
            scanf("%s" , str[i] );
            //Ins( c ,1 );
        }
        char tmp[55];
        for ( int i =0  ;i < n; i ++ )
        {
            Ins( str[i] , 1 );
            for ( int j = 0; j < n; j ++ )
            {
                if( strstr ( str[i] , str[j] ) && strcmp ( str[i], str[j] ) )
                {
                    char * pstr = strstr( str[i] , str[j] );
                    pstr += strlen ( str[j] );
                    //cout << *pstr << endl;
                    //cout << str[i] << endl;
                    //cout << str[j] << endl;
                    //cout << "here" << endl;
                    //char * ps = str[j] ;
                    int k = 0 ;
                    for ( char *ps = str[i]  ; ps != pstr ; ps ++ , k ++ )
                        tmp[k] = *ps;//cout << *ps <<1 <<  endl,getchar();
                    tmp[k] = '\0';
                    //cout << tmp << endl;
                    Ins ( tmp , 1 );
                }
            }
        }
        scanf("%s" , p );
        AC();
        int len = strlen ( p );

        for ( int i = 0 ; i <= len ; i ++ )
            for ( int j = 0 ; j < cnt ; j ++ )
                dp[i][j] = inf;
        dp[0][0] = 0;
        for ( int i = 0 ;  i <= len; i ++ )
            for ( int j = 0 ; j < cnt; j ++ )
            {
                if ( word[j] )
                    continue;
                int temp = 0;
                for ( int k = 0 ; k < 4 ; k ++ )
                {
                    temp = chd[j][k];
                    if ( sw[p[i]] == k )
                        if ( word[temp] )
                            dp[i+1][temp] = inf;
                        else
                            dp[i+1][temp] = min( dp[i][j] , dp[i+1][temp] );
                    else
                        if ( !word[temp])
                            dp[i+1][temp] = min ( dp[i][j] +1 , dp[i+1][temp] );
                }
            }
        int ans = inf;
        for ( int i = 0 ; i < cnt ; i ++ )
            if ( !word[i] )
                ans = min( dp[len][i] , ans );
        //for ( int i = 0 ; i < cnt ; i ++ )
        //    cout << dp[len][i] << endl;
//        for ( int i = 0 ; i <= len ; i ++ )
//            for ( int j = 0 ; j < cnt ; j ++ )
//                printf("dp[%d][%d] = %d\n" , i , j   , dp[i][j] );
        if ( ans >= inf )
            ans = -1 ;
        cout << "Case " << Case << ": " << ans << endl;
    }
    return 0;
}

阅读更多
文章标签: c
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭