#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+7;
const int M = 2e6+7;
const int mod = 1e9+7;
const int base = 33;
const int maxn = 2e3+7;
using ll = long long;
int n,m, cnt = 0, cnt1 = 0, max_len = 0;
int T[M][30], T1[M][30], cntpre[M],cntpre1[M];
bool isEnd[M],isEnd1[M];
ll ha1[maxn][maxn],ha2[maxn][maxn],h[maxn];
string s[N];
void add(string s)
{
int cur = 0;
for(char c : s)
{
int k = c - 'A';
if(!T[cur][k])
{
T[cur][k] = ++cnt;
}
cur = T[cur][k];
cntpre[cur]++;
}
isEnd[cur] = 1;
}
void add1(string s)
{
int cur = 0;
for(int i = s.size() - 1; i >= 0; i--)
{
char c = s[i];
int k = c - 'A';
if(!T1[cur][k])
{
T1[cur][k] = ++cnt1;
}
cur = T1[cur][k];
cntpre1[cur]++;
}
isEnd1[cur] = 1;
}
int cnt_pre(string s)
{
int cur = 0;
for(char c : s)
{
int k = c - 'A';
if(!T[cur][k]) return 0;
cur = T[cur][k];
}
return cntpre[cur];
}
int cnt_pre1(string s)
{
int cur = 0;
for(int i = s.size() - 1; i >= 0; i--)
{
char c = s[i];
int k = c - 'A';
if(!T1[cur][k]) return 0;
cur = T1[cur][k];
}
return cntpre1[cur];
}
ll get1(int i, int l, int r)
{
return (ha1[i][r] - ha1[i][l-1]*h[r-l+1] + 1ll*mod*mod)%mod;
}
ll get2(int i, int l, int r)
{
return (ha2[i][r] - ha2[i][l-1]*h[r-l+1] + 1ll*mod*mod)%mod;
}
void sub1()
{
int max_len = 0;
for(int i = 1; i <= n; i++)
{
max_len = max(max_len,(int)s[i].size());
}
for(int i = 1; i <= n; i++)
{
ha1[i][0] = 0;
string st = s[i];
int sz = s[i].size();
s[i] = " " + s[i];
reverse(st.begin(),st.end());
st = " " + st;
for(int j = 1; j <= sz; j++)
{
ha1[i][j] = (ha1[i][j-1]*base + s[i][j])%mod;
ha2[i][j] = (ha2[i][j-1]*base + st[j])%mod;
}
}
h[0] = 1;
for(int i = 1; i <= max_len; i++)
{
h[i] = (h[i-1]*base)%mod;
}
for(int i = 1; i <= m; i++)
{
string s1,s2;
cin>>s1>>s2;
int sz1 = s1.size(), sz2 = s2.size();
reverse(s2.begin(),s2.end());
s1 = " " + s1, s2 = " " + s2;
ll ha1 = 0, ha2 = 0;
for(int j = 1; j <= sz1; j++)
{
ha1 = (ha1*base + s1[j])%mod;
}
for(int j = 1; j <= sz2; j++)
{
ha2 = (ha2*base + s2[j])%mod;
}
int cnt = 0;
for(int i1 = 1; i1 <= n; i1++)
{
int sz = s[i1].size() - 1;
if(get1(i1,1,sz1) == ha1 && get2(i1,1,sz2) == ha2) cnt++;
}
cout<<cnt<<"\n";
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
// freopen("RNA.inp","r",stdin);
// freopen("RNA.out","w",stdout);
cin>>n>>m;
for(int i = 1; i <= n; i++)
{
cin>>s[i];
add(s[i]);
add1(s[i]);
}
sub1();
}
Compilation message
selling_rna.cpp: In function 'void sub1()':
selling_rna.cpp:134:17: warning: unused variable 'sz' [-Wunused-variable]
134 | int sz = s[i1].size() - 1;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19292 KB |
Output is correct |
2 |
Correct |
3 ms |
19292 KB |
Output is correct |
3 |
Correct |
3 ms |
19292 KB |
Output is correct |
4 |
Correct |
3 ms |
19292 KB |
Output is correct |
5 |
Correct |
3 ms |
19548 KB |
Output is correct |
6 |
Correct |
3 ms |
19352 KB |
Output is correct |
7 |
Correct |
3 ms |
19544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
212 ms |
308940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
404 ms |
596048 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
19292 KB |
Output is correct |
2 |
Correct |
3 ms |
19292 KB |
Output is correct |
3 |
Correct |
3 ms |
19292 KB |
Output is correct |
4 |
Correct |
3 ms |
19292 KB |
Output is correct |
5 |
Correct |
3 ms |
19548 KB |
Output is correct |
6 |
Correct |
3 ms |
19352 KB |
Output is correct |
7 |
Correct |
3 ms |
19544 KB |
Output is correct |
8 |
Incorrect |
212 ms |
308940 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |