HDU5384 Danganronpa (2015-Year School competition, 8th game)

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Problem-Solving ideas: Use the dictionary tree to solve. The total number of occurrences of all B[j] found in a[i]. Then we can build a dictionary tree and insert all the b[j] into the dictionary tree, because all strings of a string correspond to the prefixes of all its suffixes. Therefore, in the search time, only need to find each suffix of a[i], and then add the prefix number of the suffix, you can get the number of substrings of the suffix, all suffixes of the value added, is the final answer.

3. Code:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <algorithm> #include <cassert> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include <queue> #include <deque> #include <cstdlib> #include < cstdio> #include <cstring> #include <cmath> #include <ctime> #include <cctype> #include < Functional>using namespace Std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;typedef PA     IR <int, int> p;const int n=100000+10;int n,m;char a[n][10005];char b[n];struct trie{int tree[N][26],val[N],cnt;    Trie () {init ();        } void Init () {cnt=1;    memset (tree,0,sizeof (tree));        } void Insert (char*s) {int P=0,l=strlen (s);            for (int i=0;i<l;i++) {int a=s[i]-' a ';                if (!tree[p][a]) {val[cnt]=0; Tree[p][a]=cnt++;        } P=tree[p][a];    } ++val[p];        } int Query (char*s) {int P=0,l=strlen (s), ans=0;            for (int i=0;i<l;i++) {int a=s[i]-' a ';            if (!tree[p][a]) return ans;            P=tree[p][a];        ANS+=VAL[P];    } return ans;    }}t;int Main () {int T;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&m);        T.init ();        for (int i=0;i<n;i++) scanf ("%s", A[i]);        ll ans;            for (int i=0;i<m;i++) {scanf ("%s", B);            T.insert (B);//Insert All B[j] into the Dictionary tree} for (int i=0;i<n;i++) {ans=0;            int L=strlen (a[i]); for (int j=0;j<l;j++) ans+=t.query (a[i]+j), the number of times the prefix of the enumeration suffix is present in the dictionary tree, and the return value of the suffix is given, and the summation is the answer printf ("%i64d\n", A        NS); }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU5384 Danganronpa (2015-Year School competition, 8th game)

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.