Codeforces Round #291 (Div. 2) --- C. Watto and mechanic,

Source: Internet
Author: User

Codeforces Round #291 (Div. 2) --- C. Watto and mechanic,

Watto, the owner of a spare parts store, has recently got an order for the mechanic that can process strings in a certain way. initially the memory of the mechanic is filled with n strings. then the mechanic shocould be able to process queries of the following type: "Given string s, determine if the memory of the mechanic contains string t that consists of the same number of characters as s and differs from s in exactly one position ".

Watto has already compiled the mechanic, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. he decided to entrust this job to you.
Input

The first line contains two non-negative numbers n and m (0 records ≤ limit n records ≤ limit 3 · 105, 0 records ≤ limit m records ≤ limit 3 · 105) -the number of the initial strings and the number of queries, respectively.

Next follow n non-empty strings that are uploaded to the memory of the mechanic.

Next follow m non-empty strings that are the queries to the mechanic.

The total length of lines in the input doesn't exceed 6 · 105. Each line consists only of letters 'A', 'B', 'C '.
Output

For each query print on a single line "YES" (without the quotes), if the memory of the mechanic contains the required string, otherwise print "NO" (without the quotes ).
Sample test (s)
Input

2 3
Aaaaa
Acacaca
Aabaa
Ccacacc
Caaac

Output

YES
NO
NO

Trie + dfs

/*************************************** * *********************************> File Name: cf291-c.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: ******************************** **************************************** /# include <map> # include <set> # include <queue> # include <stack> # include <vector> # include <cmath> # include <cstdio> # include <cstdlib> # include <cstri Ng> # include <iostream> # include <algorithm> using namespace std; const double pi = acos (-1); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair <int, int> PLL; const int N = 7*100010; char str [N]; int has_len [N]; int len; struct TRIE {TRIE * next [3]; int cnt; TRIE () {cnt = 0; next [0] = next [1] = next [2] = NULL ;}} * root; void insert (char str []) {int len = strlen (str ); TRIE * p = root; for (int I = 0; I <len; ++ I) {if (p-> next [str [I]-'a'] = NULL) {p-> next [str [I]-'a'] = new TRIE ();} p = p-> next [str [I]-'a'];} p-> cnt = 1;} bool find (TRIE * p, int cur, bool has) {if (cur = len) {return has & p-> cnt ;} bool flag = 0; for (int I = 0; I <3; ++ I) {if (p-> next [I]! = NULL) {if (I = str [cur]-'A') {flag | = find (p-> next [I], cur + 1, has );} else {if (has) {continue;} flag | = find (p-> next [I], cur + 1, 1) ;}} return flag ;} void DELETE (TRIE * p) {for (int I = 0; I <3; ++ I) {if (p-> next [I]! = NULL) {DELETE (p-> next [I]) ;}} delete p ;}int main () {int n, m; while (~ Scanf ("% d", & n, & m) {memset (has_len, 0, sizeof (has_len); root = new TRIE (); for (int I = 0; I <n; ++ I) {scanf ("% s", str); len = strlen (str); has_len [len] = 1; insert (str) ;}while (m --) {scanf ("% s", str); len = strlen (str); if (! Has_len [len]) {printf ("NO \ n"); continue;} if (find (root, 0, 0) {printf ("YES \ n ");} else {printf ("NO \ n") ;}} DELETE (root) ;}return 0 ;}

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.