#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
typedef pair<pii,pii> piipii;
const int N = 1e5;
const int MULT = 23;
int getNum(char x){
return x-'A';
}
pii getHash(string &s, int len){
int ret = 0;
for(int i=0;i<len;i++){
ret *= MULT;
ret += getNum(s[i]);
}
return make_pair(ret, len);
}
map<int,int> memoDep[N+5];
map<int,int> memoBel[N+5];
string li[N+5];
int n,m;
string pre[N+5], suf[N+5];
set<pii> isi;
set<int> dep, bel;
map<piipii, int> cnt;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> m;
for(int i=1;i<=n;i++){
cin >> li[i];
}
for(int i=1;i<=m;i++){
cin >> pre[i] >> suf[i];
reverse(suf[i].begin(), suf[i].end());
isi.insert(make_pair(pre[i].size(), suf[i].size()));
dep.insert(pre[i].size());
bel.insert(suf[i].size());
cnt[make_pair(getHash(pre[i], pre[i].size()), getHash(suf[i], suf[i].size()))] = 0;
}
for(int i=1;i<=n;i++){
int tmp = 0;
int prev = 0;
for(auto x: dep){
for(;prev<x;prev++){
tmp *= MULT;
tmp += getNum(li[i][prev]);
}
memoDep[i][x] = tmp;
}
}
for(int i=1;i<=n;i++){
int tmp = 0;
int prev = 0;
string tmps = li[i];
reverse(tmps.begin(), tmps.end());
for(auto x: bel){
for(;prev<x;prev++){
tmp *= MULT;
tmp += getNum(tmps[prev]);
}
memoBel[i][x] = tmp;
}
}
for(int i=1;i<=n;i++){
for(auto cand: isi){
piipii tmp = make_pair(make_pair(memoDep[i][cand.first], cand.first), make_pair(memoBel[i][cand.second], cand.second));
if(cnt.find(tmp) != cnt.end()) cnt[tmp]++;
}
}
for(int i=1;i<=m;i++){
cout << cnt[make_pair(getHash(pre[i], pre[i].size()), getHash(suf[i], suf[i].size()))] << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19156 KB |
Output is correct |
2 |
Correct |
11 ms |
19064 KB |
Output is correct |
3 |
Correct |
13 ms |
19112 KB |
Output is correct |
4 |
Correct |
11 ms |
19156 KB |
Output is correct |
5 |
Correct |
13 ms |
19096 KB |
Output is correct |
6 |
Correct |
12 ms |
19092 KB |
Output is correct |
7 |
Correct |
11 ms |
19108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1041 ms |
150788 KB |
Output is correct |
2 |
Execution timed out |
1584 ms |
276532 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
24500 KB |
Output is correct |
2 |
Correct |
130 ms |
36400 KB |
Output is correct |
3 |
Correct |
138 ms |
38440 KB |
Output is correct |
4 |
Correct |
63 ms |
22764 KB |
Output is correct |
5 |
Correct |
150 ms |
36500 KB |
Output is correct |
6 |
Correct |
159 ms |
38448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
19156 KB |
Output is correct |
2 |
Correct |
11 ms |
19064 KB |
Output is correct |
3 |
Correct |
13 ms |
19112 KB |
Output is correct |
4 |
Correct |
11 ms |
19156 KB |
Output is correct |
5 |
Correct |
13 ms |
19096 KB |
Output is correct |
6 |
Correct |
12 ms |
19092 KB |
Output is correct |
7 |
Correct |
11 ms |
19108 KB |
Output is correct |
8 |
Correct |
1041 ms |
150788 KB |
Output is correct |
9 |
Execution timed out |
1584 ms |
276532 KB |
Time limit exceeded |
10 |
Halted |
0 ms |
0 KB |
- |