fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #pragma GCC optimize("O2")
  4. using ll=long long;
  5. using ull=unsigned long long;
  6. #define fn "SUGGEST"
  7. #define nl '\n'
  8. #define rs resize
  9. #define kt size
  10. #define F first
  11. #define S second
  12. #define all(x) (x).begin(),(x).end()
  13. #define rall(x) (x).rbegin(),(x).rend()
  14. unordered_map<string,int> mp;
  15. vector<string> a;
  16. int q;
  17. struct query {
  18. int tp;
  19. string s;
  20. int x;
  21. };
  22. vector<query> Q;
  23. bool ktra(string t, string s) {
  24. if (t.kt()>s.kt()) return false;
  25. for (int i=0;i<(int)t.kt();i++) {
  26. if (t[i]!=s[i]) return false;
  27. }
  28. return true;
  29. }
  30. void sub1() {
  31. for (auto z:Q) {
  32. if (z.tp==1) {
  33. a.push_back(z.s);
  34. mp[z.s]=z.x;
  35. }
  36. else {
  37. int mx=-1;
  38. string ans="-1";
  39. for (string v:a) {
  40. if (ktra(z.s,v)) {
  41. if (mp[v]>mx) {
  42. mx=mp[v];
  43. ans=v;
  44. }
  45. else if (mp[v]==mx) {
  46. ans=min(ans,v);
  47. }
  48. }
  49. }
  50. cout << ans << nl;
  51. }
  52. }
  53. }
  54.  
  55. void sub2() {
  56. int mx[26];
  57. string ans[26];
  58. fill(mx,mx+26,-1);
  59. for (auto z:Q) {
  60. if (z.tp==1) {
  61. string s=z.s;
  62. int x=z.x;
  63. int c=s[0]-'a';
  64. if (x>mx[c] ||
  65. (x==mx[c] && (ans[c]=="" || s<ans[c]))) {
  66. mx[c]=x;
  67. ans[c]=s;
  68. }
  69. }
  70. else {
  71. int c=z.s[0]-'a';
  72. if (ans[c]=="") cout << -1 << nl;
  73. else cout << ans[c] << nl;
  74. }
  75. }
  76. }
  77.  
  78. int main() {
  79. ios_base::sync_with_stdio(false);
  80. cin.tie(nullptr);
  81. if (FILE *f=fopen(fn".INP","r")) {
  82. fclose(f);
  83. freopen(fn".INP","r",stdin);
  84. freopen(fn".OUT","w",stdout);
  85. }
  86. cin >> q;
  87. bool ok2=true;
  88. for (int i=1;i<=q;i++) {
  89. int tp;
  90. cin >> tp;
  91. if (tp==1) {
  92. string s;
  93. int x;
  94. cin >> s >> x;
  95. Q.push_back({tp,s,x});
  96. }
  97. else {
  98. string p;
  99. cin >> p;
  100. if (p.kt()!=1) ok2=false;
  101. Q.push_back({tp,p,0});
  102. }
  103. }
  104. if (ok2) sub2();
  105. else sub1();
  106. return 0;
  107. }
  108.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Standard output is empty