結果

問題 No.2020 Sum of Common Prefix Length
ユーザー 👑 potato167potato167
提出日時 2022-07-22 22:20:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,362 bytes
コンパイル時間 2,451 ms
コンパイル使用メモリ 211,860 KB
実行使用メモリ 7,836 KB
最終ジャッジ日時 2023-09-17 10:43:52
合計ジャッジ時間 26,930 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 WA -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 WA -
testcase_15 TLE -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}


namespace po167{
template<int char_size,int base>
struct Trie_Tree
{
	struct Node{
		std::vector<int> next_node;
		std::vector<int> accept_node;
		int parent_node;
		int char_number;
		int cou;
		Node(int c_):next_node(char_size),char_number(c_),parent_node(-1),cou(0){
			for(int i=0;i<char_size;i++) next_node[i]=-1;
		}
	};
	std::vector<Node> nodes;
	Trie_Tree(){
		nodes.push_back(Node(-1));
	}
	vector<int> insert(std::string &word,int word_id){
		int node_id=0;
		vector<int> ids;
		for(int i=0;i<(int)word.size();i++){
			int c=(int)(word[i]-base);
			int next_id=nodes[node_id].next_node[c];
			if(next_id==-1){
				next_id=(int)nodes.size();
				nodes.push_back(Node(c));
			}
			nodes[next_id].cou++;
			nodes[next_id].parent_node=node_id;
			nodes[node_id].next_node[c]=next_id;
			nodes[next_id].accept_node.push_back(word_id);
			node_id=next_id;
			ids.push_back(node_id);
		}
		return ids;
	}
	int min_insert(char ch,int node_id,int word_id){
		int c=(int)(ch-base);
		int next_id=nodes[node_id].next_node[c];
		if(next_id==-1){
			next_id=(int)nodes.size();
			nodes.push_back(Node(c));
		}
		nodes[next_id].cou++;
		nodes[next_id].parent_node=node_id;
		nodes[node_id].next_node[c]=next_id;
		nodes[next_id].accept_node.push_back(word_id);
		node_id=next_id;
		return node_id;
	}
};
}
using po167::Trie_Tree;




void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	int N;
	cin>>N;
	string S;
	vector<vector<int>> ids(N);
	po167::Trie_Tree<26,(int)('a')> T;
	rep(i,N){
		string S;
		cin>>S;
		ids[i]=T.insert(S,i);
		//vec_out(ids[i]);
	}
	//cout<<endl;
	int Q;
	cin>>Q;
	vector<ll> base(N);
	int B=0;
	rep(i,N){
		for(int j=B;j<(int)(ids[i].size());j++){
			base[i]+=(int)(T.nodes[ids[i][j]].accept_node.size());
		}
	}
	rep(i,Q){
		int t,ind;
		cin>>t>>ind;
		ind--;
		if(t==1){
			char c;
			cin>>c;
			int tmp=T.min_insert(c,(*ids[ind].rbegin()),ind);
			if((int)(ids[ind].size())>=B){
				for(auto x:T.nodes[tmp].accept_node) base[x]++,base[ind]++;
			}
			ids[ind].push_back(tmp);
		}else{
			ll ans=base[ind];
			rep(j,min((int)(ids[ind].size()),B)) ans+=(ll)(T.nodes[ids[ind][j]].accept_node.size());
			cout<<ans<<"\n";
		}
	}
}

0