HDU 4416 Good Article Good sentence

Description

In middle school, teachers used to encourage us to pick up pretty sentences so that we could apply those sentences in our own articles. One of my classmates ZengXiao Xian, wanted to get sentences which are different from that of others, because he thought the distinct pretty sentences might benefit him a lot to get a high score in his article. 
Assume that all of the sentences came from some articles. ZengXiao Xian intended to pick from Article A. The number of his classmates is n. The i-th classmate picked from Article Bi. Now ZengXiao Xian wants to know how many different sentences she could pick from Article A which don't belong to either of her classmates?Article. To simplify the problem, ZengXiao Xian wants to know how many different strings, which is the substring of string A, but is not substring of either of string Bi. Of course, you will help him, won't you? 
 

Input

The first line contains an integer T, the number of test data. 
For each test data 
The first line contains an integer meaning the number of classmates. 
The second line is the string A;The next n lines,the ith line input string Bi. 
The length of the string A does not exceed 100,000 characters , The sum of total length of all strings Bi does not exceed 100,000, and assume all string consist only lowercase characters 'a' to 'z'. 
 

Output

For each case, print the case number and the number of substrings that ZengXiao Xian can find.
 

Sample Input

     
     
     
     
3 2 abab ab ba 1 aaa bbb 2 aaaa aa aaa
 

Sample Output

     
     
     
     
Case 1: 3 Case 2: 3

Case 3: 1

用A串弄个后缀自动机,然后像lcs那样搞,最后统计一下个数

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn = 300005;
char s[maxn];
int T, n, t = 0;

class SAM
{
	const static int maxn = 500005;   //节点个数  
	const static int size = 26;     //字符的范围  
	const static char base = 'a';     //字符的基准  

	class node
	{
	public:
		node *fa, *next[size];
		int len, cnt;
		node* clear(int x)
		{
			fa = 0; len = x;
			cnt = 0;
			memset(next, 0, sizeof(next));
			return this;
		}
	}nd[maxn], *u[maxn];                      //节点的设置  

	node *root, *last;              //根节点,上一个节点  
	int tot, f[maxn], len;                        //总节点数  
public:
	void clear()
	{
		last = root = &nd[tot = 0];
		nd[0].clear(len = 0);
	}                               //初始化  
	void insert(char ch)
	{
		len = last->len + 1;
		node *p = last, *np = nd[++tot].clear(p->len + 1);
		last = np;
		int x = ch - base;
		while (p&&p->next[x] == 0) p->next[x] = np, p = p->fa;
		if (p == 0) { np->fa = root; return; }

		node* q = p->next[x];
		if (p->len + 1 == q->len) { np->fa = q; return; }

		node *nq = nd[++tot].clear(p->len + 1);
		for (int i = 0; i < size; i++)
		if (q->next[i]) nq->next[i] = q->next[i];
		nq->fa = q->fa;
		q->fa = np->fa = nq;
		while (p &&p->next[x] == q) p->next[x] = nq, p = p->fa;
	}                               //插入操作  

	void find(char *ch)
	{
		node *s = root;
		int maxlen = 0;
		for (int i = 0; ch[i]; i++)
		{
			int x = ch[i] - base;
			if (s->next[x])
			{
				s = s->next[x];
				s->cnt = max(s->cnt, ++maxlen);
			}
			else
			{
				while (s&&!s->next[x]) s = s->fa;
				if (s == 0) s = root, maxlen = 0;
				else
				{
					maxlen = s->len + 1; s = s->next[x];
					s->cnt = max(s->cnt, maxlen);
				}
			}
		}
	}
	void query()
	{
		for (int i = 0; i <= len; i++) f[i] = 0;
		for (int i = 1; i <= tot; i++) f[nd[i].len]++;
		for (int i = 1; i <= len; i++) f[i] += f[i - 1];
		for (int i = 1; i <= tot; i++) u[f[nd[i].len]--] = &nd[i];
		for (int i = tot; i; i--) u[i]->fa->cnt = max(u[i]->fa->cnt, u[i]->cnt);
		LL ans = 0;
		for (int i = tot; i; i--)
		{
			ans += max(0, nd[i].len - max(nd[i].cnt, nd[i].fa->len));
		}
		printf("%I64d\n", ans);
	}
}sam;

int main()
{
	scanf("%d", &T);
	while (T--)
	{
		sam.clear();
		scanf("%d%s", &n, s);
		for (int i = 0; s[i]; i++) sam.insert(s[i]);
		while (n--)
		{
			scanf("%s", s);
			sam.find(s);
		}
		printf("Case %d: ", ++t);
		sam.query();
	}
	return 0;
}


你可能感兴趣的:(HDU)