poj 3691
来源:互联网 发布:swift购物商城源码 编辑:程序博客网 时间:2024/06/13 06:51
注意当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;
}
- poj 3691
- poj 3691
- poj 3691
- poj 3691
- poj 3691 DNA repair
- POJ 3691 DNA repair
- POJ 3691 DNA repair
- POJ - 3691 DNA repair
- poj 3691 DAN repair
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- hibernate一对一懒加载
- 网络字节顺序
- 关于tomcat的server.xml的Context标签
- JAVA异常总结 - 继承
- CPU与DSP
- poj 3691
- XHTML文档类型声明
- hibernate3.6finald的Annotation实现实体类与数据库表的映射关系(不再需要hibernate-annotations.jar hibernate-commons-annota)
- KERNELRELEASE 的定义
- org.apache.catalina.core.JreMemoryLeakPreventionListener解决办法
- android 对话框Dialog背景透明
- 几多欢喜几多泪
- Java与C之间传递数据
- Ubuntu 11.10 更新源(ubuntu yuan)