# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
321198 | colazcy | Lozinke (COCI17_lozinke) | C++17 | 42 ms | 8800 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;
map<int,int> mp;
namespace ac{
constexpr int maxnode = 2e5 + 100,sigmasiz = 26;
int ch[maxnode][sigmasiz],val[maxnode],fail[maxnode],tot;
inline int idx(char c){return c - 'a';}
inline void insert(const string &str){
int u = 0;
for(auto x : str){
int c = idx(x);
if(!ch[u][c])ch[u][c] = ++tot;
u = ch[u][c];
}
val[u]++;
}
inline void build(){
queue<int> q;
for(int i = 0;i < sigmasiz;i++)
if(ch[0][i])q.push(ch[0][i]);
while(!q.empty()){
int u = q.front();q.pop();
for(int i = 0;i < sigmasiz;i++)
if(ch[u][i])fail[ch[u][i]] = ch[fail[u]][i],q.push(ch[u][i]);
else ch[u][i] = ch[fail[u]][i];
}
}
inline int query(const string &str){
mp.clear();
int u = 0;
for(auto x : str){
int c = idx(x);
u = ch[u][c];
for(int j = u;j;j = fail[j])
if(val[j])mp[j] = val[j];
}
int res = 0;
for(auto it : mp)res += it.second;
return res;
}
}
int n;
char tmp[32];
vector<string> vec;
int main(){
scanf("%d",&n);
for(int i = 1;i <= n;i++)
scanf("%s",tmp),vec.push_back(string(tmp));
for(auto x : vec)ac::insert(x);
ac::build();
int ans = 0;
for(auto x : vec)ans += ac::query(x);
printf("%d\n",ans - n);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |