Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]

Watto,一家备用零件商店的所有者,收到了一个关于机制的订单,该机制可以以特定方式处理字符串。机制内存中已上传了n个字符串,并需处理m次查询,判断是否存在与查询字符串长度相同,仅在一处字符不同的字符串存在于内存中。

传送门

C. Watto and Mechanism
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism 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 mechanism, 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 ≤ n ≤ 3·105, 0 ≤ m ≤ 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 mechanism.

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

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 mechanism 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

 

题意:n个串,m次查询,每次给一个字符串,问在原串中能不能找到一个串,与之长度相同,且只有一个字符不同。

题解:没什么好说的,字典树,强行有个数组不开成全局变量,T哭了,,,,

98567602015-02-15 11:39:10njczy2010 C - Watto and Mechanism GNU C++Accepted577 ms99188 KB
98567122015-02-15 11:34:22njczy2010 C - Watto and Mechanism GNU C++Time limit exceeded on test 32 3000 ms101200 KB
98567072015-02-15 11:33:54njczy2010 C - Watto and Mechanism GNU C++Memory limit exceeded on test 1 46 ms262100 KB
98566972015-02-15 11:32:29njczy2010 C - Watto and Mechanism GNU C++Time limit exceeded on test 32 3000 ms90700 KB
98566332015-02-15 11:26:42njczy2010 C - Watto and Mechanism GNU C++Time limit exceeded on test 32 3000 ms89500 KB
98565532015-02-15 11:17:52njczy2010 C - Watto and Mechanism GNU C++Wrong answer on test 4 15 ms70700 KB
98565172015-02-15 11:14:26njczy2010 C - Watto and Mechanism GNU C++Wrong answer on test 13 46 ms70800 KB

 

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdlib>
  4 #include<cstdio>
  5 #include<algorithm>
  6 #include<cmath>
  7 #include<queue>
  8 #include<map>
  9 #include<set>
 10 #include<stack>
 11 #include<string>
 12 
 13 #define N 300005
 14 #define M 1505
 15 //#define mod 10000007
 16 //#define p 10000007
 17 #define mod2 1000000000
 18 #define ll long long
 19 #define LL long long
 20 #define eps 1e-6
 21 //#define inf 2147483647
 22 #define maxi(a,b) (a)>(b)? (a) : (b)
 23 #define mini(a,b) (a)<(b)? (a) : (b)
 24 
 25 using namespace std;
 26 
 27 int n,m;
 28 int fff[10*N];
 29 
 30 typedef struct
 31 {
 32     char v;
 33     int mp[4];
 34 }PP;
 35 
 36 int tot;
 37 int cnt[10*N];
 38 
 39 PP p[10*N];
 40 char s[10*N];
 41 
 42 void insert(int l)
 43 {
 44     int i;
 45     int now=0;
 46     for(i=0;i<l;i++){
 47         if(p[now].mp[ s[i]-'a' ]==0){
 48             tot++;
 49             p[now].mp[ s[i]-'a' ]=tot;
 50             p[tot].v=s[i];
 51             memset(p[tot].mp,0,sizeof(p[tot].mp));
 52             now=tot;
 53         }
 54         else{
 55             now=p[now].mp[ s[i]-'a' ];
 56         }
 57     }
 58     cnt[now]++;
 59 }
 60 
 61 void ini()
 62 {
 63     memset(fff,0,sizeof(fff));
 64     int i;
 65     int l;
 66     tot=0;
 67     p[0].v='z';
 68     memset(p[0].mp,0,sizeof(p[0].mp));
 69 
 70     for(i=1;i<=n;i++){
 71         scanf("%s",s);
 72         l=strlen(s);
 73         fff[l]=1;
 74         insert(l);
 75     }
 76     //for(i=0;i<=tot;i++){
 77    //     printf(" i=%d v=%c cnt=%d\n",i,p[i].v,cnt[i]);
 78    // }
 79 }
 80 
 81 int check(int l,int cou,int now,int f)
 82 {
 83    // printf(" l=%d cou=%d now=%d v=%c cnt=%d f=%d\n",l,cou,now,p[now].v,cnt[now],f);
 84     if(cou==l){
 85         if(cnt[now]==0) return 0;
 86         if(f==1) return 1;
 87         else return 0;
 88     }
 89     if(f>=2) return 0;
 90     int i;
 91     int ff;
 92     for(i=0;i<=2;i++){
 93         if(p[now].mp[i]!=0){
 94             if(s[cou]==i+'a'){
 95                 ff=check(l,cou+1,p[now].mp[i],f);
 96             }
 97             else{
 98                 ff=check(l,cou+1,p[now].mp[i],f+1);
 99             }
100             if(ff==1) return 1;
101         }
102     }
103     return 0;
104 }
105 
106 void solve()
107 {
108     int i;
109     int l;
110     int flag;
111     for(i=1;i<=m;i++){
112         scanf("%s",s);
113         l=strlen(s);
114        // printf("l=%d\n",l);
115         if(fff[l]==0){
116             flag=0;
117         }
118         else{
119             flag=check(l,0,0,0);
120         }
121         if(flag==1){
122             printf("YES\n");
123         }
124         else{
125             printf("NO\n");
126         }
127     }
128 }
129 
130 void out()
131 {
132 
133 }
134 
135 int main()
136 {
137     //freopen("data.in","r",stdin);
138     //freopen("data.out","w",stdout);
139     //scanf("%d",&T);
140     //for(int ccnt=1;ccnt<=T;ccnt++)
141     //while(T--)
142     //scanf("%d%d",&n,&m);
143     while(scanf("%d%d",&n,&m)!=EOF)
144     {
145         ini();
146         solve();
147         out();
148     }
149     return 0;
150 }

 

转载于:https://www.cnblogs.com/njczy2010/p/4293135.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值