답안 #321198

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
321198 2020-11-11T13:17:46 Z colazcy Lozinke (COCI17_lozinke) C++17
100 / 100
42 ms 8800 KB
#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

lozinke.cpp: In function 'int main()':
lozinke.cpp:50:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   50 |  scanf("%d",&n);
      |  ~~~~~^~~~~~~~~
lozinke.cpp:52:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   52 |   scanf("%s",tmp),vec.push_back(string(tmp));
      |   ~~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 0 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 1 ms 360 KB Output is correct
5 Correct 2 ms 620 KB Output is correct
6 Correct 3 ms 748 KB Output is correct
7 Correct 4 ms 1132 KB Output is correct
8 Correct 2 ms 1260 KB Output is correct
9 Correct 21 ms 2404 KB Output is correct
10 Correct 11 ms 4452 KB Output is correct
11 Correct 31 ms 3684 KB Output is correct
12 Correct 23 ms 8036 KB Output is correct
13 Correct 20 ms 3296 KB Output is correct
14 Correct 42 ms 8416 KB Output is correct
15 Correct 28 ms 8800 KB Output is correct
16 Correct 12 ms 1636 KB Output is correct
17 Correct 10 ms 1636 KB Output is correct
18 Correct 11 ms 1636 KB Output is correct
19 Correct 25 ms 5856 KB Output is correct
20 Correct 30 ms 1636 KB Output is correct