# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
1084071 | pndhpndh | Rima (COCI17_rima) | C++14 | 388 ms | 162720 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
int n, m;
const int mx=1e6+5;
const int inf=1e18;
int dp[mx];
void read(){
}
struct Trie{
struct Trie* child[26]; int cnt;int ed;
};
Trie* Node(){
struct Trie* root = NULL;
root = (Trie*)malloc(sizeof(struct Trie));
for(int i=0;i<=25;i++) root->child[i] = NULL;
root->cnt = -1;
return root;
}
int res=1;
void addstr(Trie* root, string s){
for(int i=0;i+1<s.size();i++){
char f=s[i];
int c = f - 'a';
if(root->child[c] == NULL){
root->child[c] = Node();
}
root = root->child[c];
}
if (root->cnt==-1){
root->cnt=0;
}
char f=s[s.size()-1];
int c = f - 'a';
if(root->child[c] == NULL){
root->child[c] = Node();
}
root = root->child[c];
root->cnt=1;
// cout << root->cnt << '\n';
}
int dfs(Trie* root){
int ans=root->cnt;
// cout << ans << '\n';
for (int i=0;i<=25;i++){
Trie* temp=root;
if (root->child[i]!=NULL){
temp=temp->child[i];
// cout << (char)(i+'a') << " ";
int a=dfs(temp);
// cout << ans << " " << a << " " << (char)(i+'a') << '\n';
if (temp->cnt==1){
ans+=a;
}
// cout << ans << " " << a << " " << (char)(i+'a') << '\n';
}
}
res=max(res,ans);
// std::cout << ans << '\n';
return ans;
}
bool cmp(string a,string b){
if (a.size()==b.size()){
for (int i=0;i<a.size();i++){
if (a[i]!=b[i]){
return a[i]<b[i];
}
}
}
return a.size()<b.size();
}
void solve(){
int n;
cin >> n;
string s;
Trie* root = Node();
vector<string> vect;
root->cnt=0;
for (int i=1;i<=n;i++){
cin >> s;
reverse(s.begin(),s.end());
vect.pb(s);
}
sort(vect.begin(),vect.end(),cmp);
vect.erase(unique(vect.begin(),vect.end()),vect.end());
for (int i=0;i<vect.size();i++){
addstr(root,vect[i]);
}
dfs(root);
cout << res;
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
read();
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |