• [Usaco2012 Jan]Video Game


    Description
    Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the only valid buttons. Bessie may press the buttons in any order she likes; however, there are only N distinct combos possible (1 <= N <= 20). Combo i is represented as a string S_i which has a length between 1 and 15 and contains only the letters 'A', 'B', and 'C'. Whenever Bessie presses a combination of letters that matches with a combo, she gets one point for the combo. Combos may overlap with each other or even finish at the same time! For example if N = 3 and the three possible combos are "ABA", "CB", and "ABACB", and Bessie presses "ABACB", she will end with 3 points. Bessie may score points for a single combo more than once. Bessie of course wants to earn points as quickly as possible. If she presses exactly K buttons (1 <= K <= 1,000), what is the maximum number of points she can earn?

    给出n个ABC串combo[1..n]和k,现要求生成一个长k的字符串S,问S与word[1..n]的最大匹配数

    Input
    Line 1: Two space-separated integers: N and K. * Lines 2..N+1: Line i+1 contains only the string S_i, representing combo i.

    Output
    Line 1: A single integer, the maximum number of points Bessie can obtain.

    Sample Input
    3 7
    ABA
    CB
    ABACB

    Sample Output
    4


    首先对所有的得分串建立AC自动机,然后考虑dp,设(f[i][j])表示当前长度为(i),匹配到AC自动机上节点(j)的得分,转移直接枚举(j)之后连的字符即可

    然后建fail指针的时候把终止标识符累加起来,这样之后就可以(O(1))询问了

    /*program from Wolfycz*/
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    inline int frd(){
    	int x=0,f=1;char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline int read(){
    	int x=0,f=1;char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)    putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    const int N=3e2,M=1e3;
    struct S1{
    	int trie[N+10][3],fail[N+10],End[N+10],tot,root;
    	void insert(char *s){
    		int len=strlen(s),p=root;
    		for (int i=0;i<len;i++){
    			if (!trie[p][s[i]-'A'])	trie[p][s[i]-'A']=++tot;
    			p=trie[p][s[i]-'A'];
    		}
    		End[p]++;
    	}
    	void make_fail(){
    		static int h[N+10];
    		int head=1,tail=0;
    		for (int i=0;i<3;i++)	if (trie[root][i])	h[++tail]=trie[root][i];
    		for (;head<=tail;head++){
    			int Now=h[head];
    			End[Now]+=End[fail[Now]];//累计标识符
    			for (int i=0;i<3;i++){
    				if (trie[Now][i]){
    					int son=trie[Now][i];
    					fail[son]=trie[fail[Now]][i];
    					h[++tail]=son;
    				}else	trie[Now][i]=trie[fail[Now]][i];
    			}
    		}
    	}
    }AC;//Aho-Corasick automation
    int f[M+10][N+10];
    int main(){
    	int n=read(),K=read();
    	for (int i=1;i<=n;i++){
    		static char s[20];
    		scanf("%s",s);
    		AC.insert(s);
    	}
    	AC.make_fail();
    	memset(f,255,sizeof(f));
    	f[0][0]=0;
    	for (int i=0;i<K;i++){
    		for (int j=0;j<=AC.tot;j++){
    			if (!~f[i][j])	continue;
    			for (int k=0;k<3;k++){
    				int tmp=AC.trie[j][k];
    				f[i+1][tmp]=max(f[i+1][tmp],f[i][j]+AC.End[tmp]);
    			}
    		}
    	}
    	int Ans=0;
    	for (int i=0;i<=AC.tot;i++)	Ans=max(Ans,f[K][i]);
    	printf("%d
    ",Ans);
    	return 0;
    }
    
  • 相关阅读:
    Improving the Safety, Scalability, and Efficiency of Network Function State Transfers
    MacOS下安装BeautifulSoup库及使用
    python爬虫调用搜索引擎及图片爬取实战
    2018软工项目UML设计(团队)
    【计算几何】如何计算两个圆的交点坐标
    【POJ 】POJ 3281. Dining
    【POJ 3233】矩阵多项式和
    【Effective C++】Effective C++笔记
    【图形学】图形学的23个基础问题
    【Google Code Jam】Millionaire
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/10485743.html
Copyright © 2020-2023  润新知