bzoj2754 "SCOI2012" the name on the Meow planet

Source: Internet
Author: User

2754: [SCOI2012] The name on the Meow Planet time limit: Sec Memory Limit: MB
Submit: 1577 Solved: 710
[Submit] [Status] [Discuss] Descriptiona180285 was fortunate to have been chosen as an international student for the Earth to meow. He found that the cat's name before class was notoften interesting. Suppose there are n cats in the class, and each meow name consists of a surname and a name. The teacher on the Mew planet will choosem-String to call, each time a string is read out, if the string is a MEW surname or a substring of the name, thenthe meow must answer. However, because the loadline of the Mew are too eccentric to be expressed in ASCII. For convenience of description,a180285 decided to use several strings to denote the name of the Mew. Now you can help a180285 to count the number of cats answered at each call, and the end of the M-roll.How many times does each meow answer? Inputnow define the string given method on the Meow Planet:A positive integer L is given, representing the length of the string, followed by an L integer representing each character of the string. the first line of input is two integers n and M. Next there are n rows, each containing the first and last names of the I-Cat and two strings. The surname and the names are standard meow on the planetstring. next there are M lines, each containing a string on the Meow planet, indicating the string the teacher named. Output for each teacher named string output How many cats should answer. the last line then outputs how many times each meow is ordered. Sample Input2 3
6 8 25 0 24 14 8 6 18 0 10 20 24 0
7 14 17 8 7 0 17 0 5 8 25 0 24 0
4 8 25) 0 24
4 7 0) 17 0
4 17 0) 8 25
Sample Output
2
1
0
1 2
Prompted
The fact that the data given in the sample is translated into the language of the earth can be seen
2 3
Izayoi Sakuya
Orihara Izaya
Izay
Hara
RaizHINT



"Data Range"

For 30% of data, ensure that:

1<=n,m<=1000, the names of the cats are not more than 4000, the total length of the name string does not exceed 2000.

For 100% of data, ensure that:

1<=n<=20000,1<=m<=50000, the total length of the cat's name and the total length of the count are not more than 100000, ensuring that the number of characters in the Meow string is not more than 10000.




AC Automatic Machine

A little trick: for a cat, the surname and the name of the substring meet the requirements, you can put the first name and the name together, the middle plus a number that did not appear. This turns the problem of two strings into a string.

The character set is too large to be stored in map.

Initialization is too slow, the vector is used to save the modified location, after the calculation and then modify.

Then there is the basic question: give a string, give a string s, and ask how many strings contain the substring s.

The method is to put the pattern string into an AC automaton (similar to KMP) and then run the text string.




#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring > #include <algorithm> #include <queue> #include <vector> #include <map> #define F (i,j,n) for ( int i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) #define LL long long#define maxn 100005using namespace Std;i NT N,m,cnt=1,ans1[maxn],ans2[maxn],fail[maxn];bool vst[maxn],mark[maxn];vector<int> A[maxn],tag[maxn],V,M; map<int,int> t[maxn];queue<int> q;inline int read () {int X=0,f=1;char ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} void insert (int id) {int len=read (), x,now=1; F (I,1,len) {x=read (); if (!t[now][x]) t[now][x]=++cnt;now=t[now][x];} Tag[now].push_back (ID);} void Getfail () {Q.push (1), while (!q.empty ()) {int X=q.front (), Q.pop (); for (Map<int,int>::iterator I=t[x].begin ( ); I!=t[x].end (); i++) {int tmp=i->first,y=i->second,j=fail[x];while (J&&!t[j][tmp]) J=fail[j];fail[y]=j?t[j][tmp]:1;q.push (y);}} void Getans (int id,int x) {for (int i=x;i;i=fail[i]) if (!vst[i]) {vst[i]=true; V.push_back (i); for (int j=0;j<tag[i].size (); j + +) if (!mark[tag[i][j]]) {mark[tag[i][j]]=true; M.push_back (Tag[i][j]); ans1[tag[i][j]]++;ans2[id]++;}} void solve (int id) {int now=1;for (int i=0;i<a[id].size (); i++) {int x=a[id][i];while (now&&!t[now][x]) now= Fail[now];now=now?t[now][x]:1;getans (Id,now);} for (int i=0;i<v.size (); i++) vst[v[i]]=false;for (int i=0;i<m.size (); i++) Mark[m[i]]=false; V.clear (); M.clear ();} int main () {n=read (); M=read (); int len,x; F (i,1,n) {len=read (); F (J,1,len) X=read (), A[i].push_back (x); A[i].push_back (100001); Len=read (); F (J,1,len) X=read (), A[i].push_back (x);} F (i,1,m) insert (i); Getfail (); F (i,1,n) solve (i); F (i,1,m) printf ("%d\n", Ans1[i]); F (i,1,n-1) printf ("%d", Ans2[i]);p rintf ("%d\n", Ans2[n]); return 0;}


bzoj2754 "SCOI2012" the name on the Meow planet

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.