fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long int ll;
  5. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  6.  
  7. #include <ext/pb_ds/assoc_container.hpp>
  8. #include <ext/pb_ds/tree_policy.hpp>
  9. using namespace __gnu_pbds;
  10.  
  11. typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>order_set;
  12. typedef pair<int, int>pr;
  13. #define all(i) i.begin() , i.end()
  14. #define ft first
  15. #define sn second
  16. #define pb push_back
  17. #define totalone(mask) __builtin_popcount(mask)
  18. #define chkbit(mask,bit) (mask&(1LL << bit))
  19.  
  20. // debug section start
  21. void __print(int x) {cerr << x;}
  22. void __print(long x) {cerr << x;}
  23. void __print(long long x) {cerr << x;}
  24. void __print(unsigned x) {cerr << x;}
  25. void __print(unsigned long x) {cerr << x;}
  26. void __print(unsigned long long x) {cerr << x;}
  27. void __print(float x) {cerr << x;}
  28. void __print(double x) {cerr << x;}
  29. void __print(long double x) {cerr << x;}
  30. void __print(char x) {cerr << '\'' << x << '\'';}
  31. void __print(const char *x) {cerr << '\"' << x << '\"';}
  32. void __print(const string &x) {cerr << '\"' << x << '\"';}
  33. void __print(bool x) {cerr << (x ? "true" : "false");}
  34.  
  35. template<typename T, typename V>
  36. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  37. template<typename T>
  38. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  39. void _print() {cerr << "]\n";}
  40. template <typename T, typename... V>
  41. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  42. #ifndef ONLINE_JUDGE
  43. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  44. #else
  45. #define debug(x...)
  46. #endif
  47. // debug section closed
  48.  
  49. #define en "\n"
  50. #define sum(n) ((1LL*(n)*(n+1))/ 2LL)
  51. #define sqr(n) (1LL*(n)*(n))
  52. #define vag(a,b) ((a + b - 1)/b)
  53.  
  54. #define MAXN 200005
  55. #define inf 1e6+5
  56. const int mod = 1e9 + 7;
  57.  
  58. struct trie
  59. {
  60. int g[101 * 51 * 27][27];
  61. int cur;
  62.  
  63. void init()
  64. {
  65. memset(g, -1, sizeof(g));
  66. cur = 0;
  67. }
  68. int Insert(string s)
  69. {
  70. int nd = 0;
  71. int cn = 0;
  72. for (int i = 0; i < s.size(); i++)
  73. {
  74. int x = s[i] - 'a';
  75. if (g[nd][x] == -1) {
  76. cn++;
  77. g[nd][x] = ++cur;
  78. }
  79. nd = g[nd][x];
  80. }
  81. return cn;
  82. }
  83.  
  84. } tri;
  85. void solve()
  86. {
  87. int n;
  88. cin >> n;
  89. string s;
  90.  
  91. tri.init();
  92. int an = n;
  93. string pre = "";
  94.  
  95. for (int i = 0; i < n; i++) {
  96. cin >> s;
  97.  
  98. int tm = tri.Insert(s);
  99. if (i == 0) {
  100. an += tm;
  101. pre = s;
  102. continue;
  103. }
  104.  
  105. if (s.size() < pre.size())
  106. {
  107. an += (tm + 2);
  108. }
  109. else {
  110. int sz = pre.size();
  111. string ss = s.substr(0, sz);
  112. if (ss == pre)
  113. {
  114. an += min(tm + 2, (int)s.size() - sz);
  115. }
  116. else {
  117. an += (tm + 2);
  118. }
  119.  
  120. }
  121. pre = s;
  122.  
  123. }
  124.  
  125. cout << an << en;
  126. }
  127.  
  128. int main()
  129. {
  130. IOS;
  131. ll t;
  132. t = 1;
  133. cin >> t;
  134.  
  135. int c = 0;
  136. while ( t-- )
  137. {
  138. // cout<<"Case "<<++c<<": ";
  139. cout << "Case #" << ++c << ": ";
  140. solve();
  141. }
  142. return 0;
  143. }
Success #stdin #stdout 0.01s 18276KB
stdin
Standard input is empty
stdout
Case #1: 0