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);
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值