QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#760145 | #9716. Code a Trie | rotcar07 | WA | 4ms | 7620kb | C++23 | 1.7kb | 2024-11-18 15:05:39 | 2024-11-18 15:05:40 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
constexpr int maxn=1e5+5;
string s[maxn];int a[maxn];
int ch[maxn][26],tot,val[maxn];
int cnt[maxn];
inline void solve(){
int n;cin>>n;vector<int> v;
for(int i=1;i<=n;i++) cin>>s[i]>>a[i],v.push_back(a[i]);
sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());
memset(val,0,sizeof(int)*(tot+1));
memset(ch,0,sizeof(ch[0])*(tot+1));tot=1;
memset(cnt,0,sizeof(int)*(v.size()+1));
for(int i=1;i<=n;i++){
a[i]=lower_bound(v.begin(),v.end(),a[i])-v.begin()+1;
cnt[a[i]]++;
int p=1;
for(auto x:s[i]){
int&c=ch[p][x-'a'];
if(!c) c=++tot;
p=c;
}
if(val[p]){
if(val[p]==a[i]) cnt[a[i]]--;
else return cout<<"-1\n",void();
}
val[p]=a[i];
}
int ans=0;
for(int i=tot;i>=1;i--){
unordered_map<int,int> mp;
for(int x:ch[i])if(x)mp[val[x]]++;
if(val[i]) mp[val[i]]++;
// for(auto [a,b]:mp) cout<<a<<' '<<b<<'\n';
if(mp.size()==1&&!mp.count(-1)) cnt[val[i]=mp.begin()->first]-=mp.begin()->second-1,ans+=(i==1);
else if(mp.size()>0){
if(val[i]){
int mx=0;
for(auto [a,b]:mp) if(a!=val[i]) mx=max(mx,b);
if(mx>1) return cout<<"-1\n",void();
}
for(auto [a,b]:mp) if(~a&&b<cnt[a]) return cout<<"-1\n",void();
ans+=max<int>(1,mp.size()-!!mp.count(-1)),val[i]=-1;
}
// cout<<i<<' '<<val[i]<<' '<<tot<<' '<<ans<<'\n';
}
cout<<ans<<'\n';
}
int main(){
int t;cin>>t;
for(int i=1;i<=t;i++) cout<<"Case #"<<i<<": ",solve();
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 7616kb
input:
6 2 aa 1 a 2 1 a 1 3 aa 1 ab 1 ac 1 2 aa 1 ac 2 3 aaa 1 ac 1 aa 3 3 aa 1 ac 1 a 3
output:
Case #1: 3 Case #2: 1 Case #3: 1 Case #4: 3 Case #5: -1 Case #6: -1
result:
ok 6 lines
Test #2:
score: -100
Wrong Answer
time: 4ms
memory: 7620kb
input:
873 1 sgaswyo 17974065 1 s 17974065 1 naa 17974065 3 m 17974065 yimyi 864021569 m 421852697 4 sawa 613320183 auqacuy 147847601 u 58500993 aesc 461860441 1 x 613320183 3 su 613320183 g 147847601 a 750104225 3 saic 589460253 i 147847601 s 3910501 1 ioqxgke 812288287 5 a 878568205 a 147847601 c 1474279...
output:
Case #1: 1 Case #2: 1 Case #3: 1 Case #4: -1 Case #5: 4 Case #6: 1 Case #7: 3 Case #8: 3 Case #9: 1 Case #10: -1 Case #11: 3 Case #12: -1 Case #13: 4 Case #14: 1 Case #15: 1 Case #16: 1 Case #17: 1 Case #18: 1 Case #19: 1 Case #20: 1 Case #21: 1 Case #22: 2 Case #23: 1 Case #24: 1 Case #25: 1 Case #...
result:
wrong answer 356th lines differ - expected: 'Case #356: -1', found: 'Case #356: 3'