Bzoj 1590: [Usaco2008 Dec]secret message secret information

Source: Internet
Author: User

1590: [Usaco2008 Dec]secret message secret information

Description

Bessie was leading the cows to flee. In order to contact, the cows send each other secret information. The information is binary, with a total of M (1≤m≤50000) bars. A strong anti-espionage John has partially intercepted this information, knowing the pre-bi (l bi≤10000) bit of the binary information of article I. He also knows that cows use N (1≤n≤50000) codes. However, he is only aware of the former CJ (1≤cj≤10000) Bit of the code of Article J. for each password J, he wanted to know how much of the truncated information would match it. That is, how much information has the same prefix as this password. Of course, the prefix length must be equal to the password and the smaller of the length of the message. in the input file, the total number of bits (that is, ∑bi+∑ci) does not exceed 500000Inputline 1th enters N and M, followed by n lines to describe the secret information, followed by the M line to describe the password. Enter an integer for each line to indicate the length of the information or password, and then enter this information or password. All numbers are separated by a space. OutputTotal m-line, output the number of matching information for each password. Sample Input4 5
3 0 1 0
1 1
3 1 0 0
3 1 1 0
1 0
1 1
2 0 1
5 0 1 0 0 1
2 1 1

INPUT DETAILS:
four messages; Five codewords.
The intercepted messages start with 010, 1, 110, and.
The possible codewords start with 0, 1, 01001, and 11.
Sample Output1
3
1
1
2
HINT

0 matches only 010:1 match 1 matches 1, and 110:3 matches-matches only 010:1 match 01001 matches 010:1 Mat CH One matches 1 and 110:2 matches

Source

Gold

Exercises

Compare the bare trie tree, use p[x].cnt to represent the number of strings at the end of X, so that only the length of the scheme is less than the current string, so then set P[x].sum to represent the X-subtree and.

The overall solution is the CNT accumulation on the path plus sum

#include <stdio.h> #include <iostream> #include <string.h>using namespace std;struct node{int next[2] , Cnt,sum;void init () {Cnt=sum=0;memset (next,-1,sizeof (Next));}} P[500005];int n,m,i,j,x,a[10005],b[10005],tot;inline void Read (int&a) {char c;while (!) ( ((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')); a=c-' 0 '; while (((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')) (a*= Ten) +=c-' 0 ';} void update (int len) {int x=0,k=0;for (int i=1;i<=len;i++) {int j=a[i];if (p[x]). Next[j]==-1) {p[x]. Next[j]=++tot;p[tot].init ();} X=P[X]. Next[j];b[++k]=x;} p[x].cnt++;for (int i=1;i<k;i++) p[b[i]].sum++;} int solve (int len) {int x=0,ans=0;for (int i=1;i<=len;i++) {int j=a[i];if (p[x]). NEXT[J]==-1) return ans;x=p[x]. next[j];ans+=p[x].cnt;} return ans+p[x].sum;} int main () {read (n), read (m);p [0].init (); for (i=1;i<=n;i++) {read (x); for (j=1;j<=x;j++) read (a[j]); update (x);} for (i=1;i<=m;i++) {read (x), for (j=1;j<=x;j++) read (A[j]);p rintf ("%d\n", Solve (x));} return 0;}

  

Bzoj 1590: [Usaco2008 Dec]secret message secret information

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.