"BZOJ-3172" Word AC automatic machine

Source: Internet
Author: User

3172: [Tjoi2013] Word time limit:10 Sec Memory limit:512 MB
submit:2567 solved:1200
[Submit] [Status] [Discuss] Description

Someone reads a paper, a paper is made up of many words. But he found that a word would appear many times in the paper, and now wondered how many times each word appeared in the paper.

Input

The first integer n, which represents how many words, and the next n rows of one word per line. Each word is made up of lowercase letters, n<=200, and the word length does not exceed 10^6

Output

outputs n integers, and the number in line I indicates how many times the first word appears in the article.

Sample Input3
A
Aa
AAASample Output6
3
1Hintsourcesolution

Acmachine template problems, and do not match.

Build the trie tree, construct the fail pointer, and in the process of building the fail pointer, accumulate the number of times you can

Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>using namespacestd;#defineN 1000010intsz,son[n][ -],fail[n],q[n],sum[n],loc[n];intNCharS[n];voidClear () {sz=1;  for(intI=0; i< -; i++) son[0][i]=1;}voidInsertint&POS) {    intx=1;  for(intI=0; I<strlen (s); i++)        {            if(!son[x][s[i]-'a']) son[x][s[i]-'a']=++sz; X=son[x][s[i]-'a']; sum[x]++; } POS=x;}voidBuildfail () {intHead=0, tail=1; q[0]=1; fail[1]=0;  while(head<tail) {            intnow=q[head++];  for(intI=0; i< -; i++)                {                    if(!son[now][i])Continue; intff=Fail[now];  while(!son[ff][i]) ff=FAIL[FF]; Fail[son[now][i]]=Son[ff][i]; Q[tail++]=Son[now][i]; }        }      for(inti=tail-1; i>=0; i--) Sum[fail[q[i] ]+=Sum[q[i]];}intMain () {clear (); scanf ("%d",&N);  for(intI=1; i<=n; i++) scanf ("%s", s), insert (Loc[i]);    Buildfail ();  for(intI=1; i<=n; i++) printf ("%d\n", Sum[loc[i]]); return 0;}

A long time ago, when using automata, there were strange errors ... interesting ... Mdzz

"BZOJ-3172" Word AC automatic machine

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.