【HDU】3341 Lost's revenge AC自动机+变进制+DP

本文深入探讨了游戏开发领域的关键技术,包括cocos2dX、unity3d、Unreal Engine等游戏引擎的应用,以及AI音视频处理在游戏中的应用,如语义识别、物体检测、语音识别等。同时,文章还涵盖了图像处理AR特效、音视频直播流媒体技术,以及嵌入式硬件和开发环境的相关内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Lost's revenge

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2798    Accepted Submission(s): 709


Problem Description
Lost and AekdyCoin are friends. They always play "number game"(A boring game based on number theory) together. We all know that AekdyCoin is the man called "nuclear weapon of FZU,descendant of Jingrun", because of his talent in the field of number theory. So Lost had never won the game. He was so ashamed and angry, but he didn't know how to improve his level of number theory.

One noon, when Lost was lying on the bed, the Spring Brother poster on the wall(Lost is a believer of Spring Brother) said hello to him! Spring Brother said, "I'm Spring Brother, and I saw AekdyCoin shames you again and again. I can't bear my believers were being bullied. Now, I give you a chance to rearrange your gene sequences to defeat AekdyCoin!".

It's soooo crazy and unbelievable to rearrange the gene sequences, but Lost has no choice. He knows some genes called "number theory gene" will affect one "level of number theory". And two of the same kind of gene in different position in the gene sequences will affect two "level of number theory", even though they overlap each other. There is nothing but revenge in his mind. So he needs you help to calculate the most "level of number theory" after rearrangement.
 

Input
There are less than 30 testcases.
For each testcase, first line is number of "number theory gene" N(1<=N<=50). N=0 denotes the end of the input file.
Next N lines means the "number theory gene", and the length of every "number theory gene" is no more than 10.
The last line is Lost's gene sequences, its length is also less or equal 40.
All genes and gene sequences are only contains capital letter ACGT.
 

Output
For each testcase, output the case number(start with 1) and the most "level of number theory" with format like the sample output.
 

Sample Input
  
3 AC CG GT CGAT 1 AA AAA 0
 

Sample Output
  
Case 1: 3 Case 2: 2
 

Author
Qinz@XDU
 

Source
HDOJ Monthly Contest – 2010.03.06

传送门:【HDU】3341 Lost's revenge

题目大意:
给你n(1 <= n <= 50)个模式串,只包含ACGT,长度不超过10。再给你一个长度为m的目标串,同样只包含ACGT。问你通过将目标串上的字符重新调整后最多能包含多少个模式串。其中同样的模式串处在两个不同的位置就记作两个。

题目分析:
这题对我来说主要的难点是变进制,其他的都不难,直到看了百科说的变进制才有灵感= =||。。。
首先将目标串上的字符分别统计个数。然后每个字符的个数+1就作为相应位置的进制(假设A最大,C次之,G再次之,T排最后),+1是为了防止其中某个数为零的情况导致重叠。假设长度为m的目标串包含num[A]个A,num[C]个C,num[G]个G,num[T]个T那么一个包含了a个A,c个C,g个G,t个T的串可以表示成一个状态:
x = a*(num[C] + 1)*(num[G] + 1)*(num[T] + 1) +
      c*(num[G] + 1)*(num[T] + 1) +
      g*(num[T] + 1)+
      t;
那么设dp[x][j]表示用了a个A,c个C,g个G,t个T后达到状态j时能包含的模式串的最大个数。
则dp[x][j] = max ( dp[pre][pre-j] + word[j] )。其中pre是所有能转移到x的串的状态,pre-j是pre对应的Trie图上的状态,word[j]表示在状态j结尾的单词数(j的所有符合条件的后缀)。
那么本题就可以这么解决了。

PS:变进制可把我坑到了,拖了N天才做掉这题。

代码如下:


#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std ;

#define CLEAR( a , x ) memset ( a , x , sizeof a )
#define CHAR( i ) for ( int i = 0 ; buf[i] ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define REPF( i , a , b ) for ( i = a ; i <= b ; ++ i )

const int MAXW = 4 ;
const int MAXN = 550 ;
const int MAXD = 15000 ;
const int MAXQ = 1000000 ;

struct Trie {
	int next[MAXN][MAXW] ;
	int fail[MAXN] ;
	int word[MAXN] ;
	int P , root ;
	int Q[MAXQ] ;
	int head , tail ;
	int dp[MAXD][MAXN] ;
	
	int newnode () {
		REP ( i , MAXW )
			next[P][i] = -1 ;
		word[P] = 0 ;
		return P ++ ;
	}
	
	void init () {
		P = 0 ;
		root = newnode () ;
	}
	
	int get ( char c ) {
		if ( c == 'A' )
			return 0 ;
		if ( c == 'C' )
			return 1 ;
		if ( c == 'G' )
			return 2 ;
		if ( c == 'T' )
			return 3 ;
	}
	
	void insert ( char buf[] ) {
		int now = root ;
		CHAR ( i ) {
			int x = get ( buf[i] ) ;
			if ( next[now][x] == -1 )
				next[now][x] = newnode () ;
			now = next[now][x] ;
		}
		word[now] ++ ;
	}
	
	void build () {
		head = tail = 0 ;
		fail[root] = root ;
		REP ( i , MAXW ) {
			if ( next[root][i] == -1 )
				next[root][i] = root ;
			else {
				fail[next[root][i]] = root ;
				Q[tail ++] = next[root][i] ;
			}
		}
		while ( head != tail ) {
			int now = Q[head ++] ;
			REP ( i , MAXW ) {
				if ( next[now][i] == -1 )
					next[now][i] = next[fail[now]][i] ;
				else {
					fail[next[now][i]] = next[fail[now]][i] ;
					if ( word[fail[next[now][i]]] )
						word[next[now][i]] += word[fail[next[now][i]]] ;
					Q[tail ++] = next[now][i] ;
				}
			}
		}
	}
	
	void solve ( int num[] ) {
		int i[MAXW] ;
		int up[MAXW] ;
		up[3] = 1 ;
		up[2] = ( num[3] + 1 ) * up[3] ;
		up[1] = ( num[2] + 1 ) * up[2] ;
		up[0] = ( num[1] + 1 ) * up[1] ;
		CLEAR ( dp , -1 ) ;
		dp[0][0] = 0 ;
		REPF ( i[0] , 0 , num[0] )
			REPF ( i[1] , 0 , num[1] )
				REPF ( i[2] , 0 , num[2] )
					REPF ( i[3] , 0 , num[3] ) {
						int x = i[0] * up[0] + i[1] * up[1] + i[2] * up[2] + i[3] * up[3] ;
						REP ( j , P )
							if ( ~dp[x][j] )
								REP ( k , MAXW )
									if ( i[k] < num[k] ) {
										int nx = up[k] + x , Next = next[j][k] ;
										dp[nx][Next] = max ( dp[nx][Next] , dp[x][j] + word[Next] ) ;
									}
					}
		int n = num[0] * up[0] + num[1] * up[1] + num[2] * up[2] + num[3] * up[3] ;
		int ans = 0 ;
		REP ( i , P )
			if ( ~dp[n][i] )
				ans = max ( ans , dp[n][i] ) ;
		printf ( "%d\n" , ans ) ;
	}
} ;

Trie ac ;
char buf[MAXN] ;
int num[MAXW] ;

void work () {
	int n , cas = 0 ;
	while ( ~scanf ( "%d" , &n ) && n ) {
		printf ( "Case %d: " , ++ cas ) ;
		ac.init () ;
		REP ( i , n ) {
			scanf ( "%s" , buf ) ;
			ac.insert ( buf ) ;
		}
		ac.build () ;
		CLEAR ( num , 0 ) ;
		scanf ( "%s" , buf ) ;
		CHAR ( i )
			++ num[ac.get ( buf[i] )] ;
		ac.solve ( num ) ;
	}
}

int main () {
	work () ;
	return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值