暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
1 /*
2 set的二分查找
3 如果数据规模小的话可以用O(n^2)的暴力想法
4 否则就只好一个一个的换(a, b, c),在set容器找相匹配的
5 */
6 #include <cstdio>
7 #include <cmath>
8 #include <string>
9 #include <cstring>
10 #include <iostream>
11 #include <algorithm>
12 #include <map>
13 #include <set>
14 #include <vector>
15 using namespace std;
16
17 int main(void)
18 {
19 //freopen ("C.in", "r", stdin);
20
21 set<string> s;
22 string tmp;
23 int n, m, mx;
24
25 while (cin >> n >> m)
26 {
27 mx = -1;
28 s.clear ();
29 for (int i=1; i<=n; ++i)
30 {
31 cin >> tmp;
32 s.insert (tmp);
33 int len = tmp.size ();
34 mx = max (mx, len);
35 }
36 if (n * m * mx < 1e8)
37 {
38 for (int j=1; j<=m; ++j)
39 {
40 cin >> tmp;
41 int cnt = 0;
42 set<string>::iterator it;
43 for (it=s.begin (); it!=s.end (); ++it)
44 {
45 if (it->size () == tmp.size ())
46 {
47 cnt = 0;
48 for (int j=0; j<it->size (); ++j)
49 {
50 if ((*it)[j] != tmp[j]) cnt++;
51 if (cnt > 1) break;
52 }
53 if (cnt == 1) break;
54 }
55 }
56 if (cnt == 1) cout << "YES" << endl;
57 else cout << "NO" << endl;
58 }
59 continue;
60 }
61
62 for (int i=1; i<=m; ++i)
63 {
64 cin >> tmp;
65 bool flag = false;
66 for (int j=0; tmp[j]!='\0'; ++j)
67 {
68 if (tmp[j] == 'a')
69 {
70 tmp[j] = 'b';
71 if (s.find (tmp) != s.end ())
72 {
73 flag = true; break;
74 }
75 tmp[j] = 'c';
76 if (s.find (tmp) != s.end ())
77 {
78 flag = true; break;
79 }
80 tmp[j] = 'a';
81 }
82 if (tmp[j] == 'b')
83 {
84 tmp[j] = 'a';
85 if (s.find (tmp) != s.end ())
86 {
87 flag = true; break;
88 }
89 tmp[j] = 'c';
90 if (s.find (tmp) != s.end ())
91 {
92 flag = true; break;
93 }
94 tmp[j] = 'b';
95 }
96 if (tmp[j] == 'c')
97 {
98 tmp[j] = 'a';
99 if (s.find (tmp) != s.end ())
100 {
101 flag = true; break;
102 }
103 tmp[j] = 'b';
104 if (s.find (tmp) != s.end ())
105 {
106 flag = true; break;
107 }
108 tmp[j] = 'c';
109 }
110 }
111 (flag) ? cout << "YES" << endl : cout << "NO" << endl;
112 }
113 }
114
115 return 0;
116 }
117
118 /*
119 YES NO
120 */
编译人生,运行世界!