微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Ural 1158. Censored! AC自动机+大数

1158. Censored!

Time limit: 2.0 second
Memory limit: 64 MB
The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also kNown as Freish) consists of exactly M letters without word breaks. So,there exist exactly N Mdifferent Freish sentences.
But after recent election of Mr. Grass Jr. as Freeland president some words offending him were declared unprintable and all sentences containing at least one of them were forbidden. The sentence S contains a word W if W is a substring of S i.e. exists such k >= 1 that S[k] = W[1],S[k+1] = W[2],...,S[k+len(W)-1] = W[len(W)],where k+len(W)-1 <= M and len(W) denotes length of W. Everyone who uses a forbidden sentence is to be put to jail for 10 years.
Find out how many different sentences can be used Now by freelanders without risk to be put to jail for using it.

Input

The first line contains three integer numbers: N - the number of letters in Freish alphabet,M - the length of all Freish sentences and P - the number of forbidden words (1 ≤ N ≤ 50,1 ≤ M ≤ 50,0 ≤ P ≤ 10).
The second line contains exactly N different characters - the letters of the Freish alphabet (all with ASCII code greater than 32).
The following P lines contain forbidden words,each not longer than min(M,10) characters,all containing only letters of Freish alphabet.

Output

Output the only integer number - the number of different sentences freelanders can safely use.

Sample

input output
3 3 3
QWE
QQ
WEE
Q
7
Problem Author: Nick Durov
Problem Source: ACM ICPC 2001. northeastern European Region,northern Subregion
AC自动机+大数
C++的程序在23组数据中出现错误, 据说是字符串的原因,我感觉不太是。z只好改用Java写。
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;

class node 
{
	static int NUM=55;
	int id=-1;
	node next[] = new node[NUM];
	node fail;
	boolean flag = false;
}
class AC
{
	node root;	
	int cnt = 0;
	void init()
	{
		cnt=0;
		root = new node();
		root.id = cnt;
	}
	void insert(String str,HashMap<Integer,Integer> h)//构建Trie树
	{
		node p = root;
		int index; 
		int l = str.length();
		
		for (int i=0;i<l;i++)
		{
			index = h.get((int)str.charat(i));
			if (p.next[index]==null)
			{
				cnt++;
				p.next[index]=new node();
				p.next[index].id=cnt;
			}
			p=p.next[index];
		}
		p.flag = true;
	}
	void build()
	{
		ArrayBlockingQueue<node> q = new ArrayBlockingQueue<node>(200);
		root.fail = null;
		q.add(root);
		while (!q.isEmpty())
		{
			node tmp = q.poll();
			node p = null;
			for (int i=0;i<node.NUM;i++)
			if (tmp.next[i]!=null)
			{
				if (tmp==root)
					tmp.next[i].fail = root;
				else
				{
					p = tmp.fail;
					while (p!=null)
					{
						if (p.next[i]!=null)
						{
							tmp.next[i].fail = p.next[i];
							if (p.next[i].flag==true)
								tmp.next[i].flag = true;
							break;
						}
						p=p.fail;
					}
					if (p==null) tmp.next[i].fail = root;
				}
				q.add(tmp.next[i]);
			}
		}
	}
	void makeMatrix(int [][] m)
	{
		ArrayBlockingQueue<node> q = new ArrayBlockingQueue<node>(200);
		q.add(root);
		node p,Now;
		while (!q.isEmpty())
		{
			Now = q.poll();
			for (int i=0;i<node.NUM;i++)
			{			
				p = Now;
				while (p.next[i]==null&&p!=root)
					p=p.fail;
				p=p.next[i];
				p = (p==null)?root:p;
				if (p.flag) continue;
				m[Now.id][p.id]++;
				if (Now.next[i]!=null)
					q.add(Now.next[i]);
			}			
		}
	}
}

public class Main {
	static AC ac;
	static int n,m,p;
	static HashMap<Integer,Integer> h;
	static public void main(String[] args)
	{
		Scanner in = new Scanner(system.in);
		n = in.nextInt();
		m = in.nextInt();
		p = in.nextInt();
		int [][]  M = new int[150][150];
		ac = new AC();
		ac.init();
		String tmp;
		tmp = in.nextLine();
		tmp = in.nextLine();
		h = new HashMap<Integer,Integer>(55);
		int l = tmp.length();
		node.NUM=l;
			for (int i=0;i<l;i++)
				h.put((int)tmp.charat(i),i);
		for (int i=0;i<p;i++)
		{
			tmp = in.nextLine();
			ac.insert(tmp,h);
		}
		ac.build();
		ac.makeMatrix(M);
		BigInteger[][] A = new BigInteger[150][150];
		BigInteger[][] ans = new BigInteger[2][150];
		int tot = ac.cnt+1;
		if (tot>105) while(true); 
		int id = 0;
		for (int i=0;i<tot;i++)
		{
			for (int j=0;j<tot;j++)
				A[i][j] = BigInteger.valueOf(M[i][j]);
			ans[id][i]=(i==0)?BigInteger.ONE:BigInteger.ZERO;
		}
		for (int t=0;t<m;t++)
		{
			id = 1-id;
				for (int j=0;j<tot;j++)
				{
					ans[id][j] = BigInteger.ZERO;
					for (int k=0;k<tot;k++)
					{
					 ans[id][j] = ans[id][j].add(ans[1-id][k].multiply(A[k][j]));
					}
				}
		}		
		BigInteger res = BigInteger.ZERO;
		for (int i=0;i<tot;i++)
			res = res.add(ans[id][i]);
		System.out.println(res);
	}
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐