#include <bits/stdc++.h>
#define int long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int (i) = 0; (i) < (n); (i)++)
using namespace std;
const double EPS = 0.00001;
const int INF = 1e9+500;
const int MOD = 1e9+7;
const int N = 55;
const int K = 1505;
const int ALPH = 26;
int n,m,q,k;
vector<vector<string> > names;
vector<vector<int> > res;
int add(int x, int y) {
if(x+y < MOD) return x+y;
return x+y-MOD;
}
int mull(int x, int y) {
return (x*y)%MOD;
}
struct TrieNode {
TrieNode *c[ALPH];
int val;
TrieNode() {
for(int i = 0; i<ALPH; i++) {
c[i] = NULL;
}
val = 0;
}
void insert(string s, int num, int index) {
if(index == s.size()) {
val = add(val,num);
return;
}
if(c[s[index] - 'a'] == NULL) {
c[s[index] - 'a'] = new TrieNode();
}
c[s[index] - 'a']->insert(s,num, index+1);
}
int query(string s, int index) {
if(index == s.size()) {
return val;
}
if(c[s[index] - 'a'] == NULL) {
return 0;
}
return c[s[index] - 'a']->query(s, index+1);
}
};
void solve() {
cin>>n>>k;
res.resize(n+1, vector<int> (k));
names.resize(n+1, vector<string> (k));
for(int i = 1; i<=n; i++) {
for(int j = 0; j<k; j++) {
cin>>names[i][j];
}
}
TrieNode *roots[N];
for(int i = 1; i<=n; i++) {
roots[i] = new TrieNode();
}
for(int i = 0; i <k; i++) {
res[1][i] = 1;
}
for(int i = 1; i<n; i++) {
for(int j = 0; j<k; j++) {
//cout<<"i:"<<i<<" j:"<<j<<"\n";
roots[i]->insert(names[i][j], res[i][j], 0);
}
for(int j = 0; j<k; j++) {
string s = names[i+1][j].substr(0,i) ,t = names[i+1][j].substr(1,i);
//cout<<"i:"<<i<<" j:"<<j<<"\n";
res[i+1][j] = roots[i]->query(s, 0);
if(s!=t) {
res[i+1][j] = add(res[i+1][j] , roots[i]->query(t, 0));
}
}
}
int ans = 0;
for(int i = 0; i<k; i++) {
ans = add(ans , res[n][i]);
}
cout<<ans<<"\n";
}
signed main() {
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
Compilation message
trener.cpp: In member function 'void TrieNode::insert(std::string, long long int, long long int)':
trener.cpp:39:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | if(index == s.size()) {
| ~~~~~~^~~~~~~~~~~
trener.cpp: In member function 'long long int TrieNode::query(std::string, long long int)':
trener.cpp:51:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | if(index == s.size()) {
| ~~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23388 KB |
Output is correct |
2 |
Correct |
23 ms |
22872 KB |
Output is correct |
3 |
Correct |
21 ms |
23384 KB |
Output is correct |
4 |
Correct |
8 ms |
1116 KB |
Output is correct |
5 |
Correct |
18 ms |
16320 KB |
Output is correct |
6 |
Correct |
19 ms |
16464 KB |
Output is correct |
7 |
Correct |
8 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
24 ms |
23388 KB |
Output is correct |
6 |
Correct |
23 ms |
22872 KB |
Output is correct |
7 |
Correct |
21 ms |
23384 KB |
Output is correct |
8 |
Correct |
8 ms |
1116 KB |
Output is correct |
9 |
Correct |
18 ms |
16320 KB |
Output is correct |
10 |
Correct |
19 ms |
16464 KB |
Output is correct |
11 |
Correct |
8 ms |
1372 KB |
Output is correct |
12 |
Correct |
326 ms |
336332 KB |
Output is correct |
13 |
Correct |
337 ms |
335700 KB |
Output is correct |
14 |
Correct |
334 ms |
335764 KB |
Output is correct |
15 |
Correct |
337 ms |
335676 KB |
Output is correct |
16 |
Correct |
119 ms |
8916 KB |
Output is correct |
17 |
Correct |
259 ms |
195928 KB |
Output is correct |
18 |
Correct |
257 ms |
198428 KB |
Output is correct |
19 |
Correct |
253 ms |
198228 KB |
Output is correct |
20 |
Correct |
248 ms |
198116 KB |
Output is correct |
21 |
Correct |
254 ms |
198292 KB |
Output is correct |
22 |
Correct |
117 ms |
8964 KB |
Output is correct |