fork(1) download
  1. //tonynater - CodeChef 2013
  2.  
  3. #include <algorithm>
  4. #include <assert.h>
  5. #include <bitset>
  6. #include <cassert>
  7. #include <climits>
  8. #include <cmath>
  9. #include <complex>
  10. #include <ctime>
  11. #include <ctype.h>
  12. #include <fstream>
  13. #include <iomanip>
  14. #include <iostream>
  15. #include <list>
  16. #include <map>
  17. #include <math.h>
  18. #include <queue>
  19. #include <set>
  20. #include <sstream>
  21. #include <stack>
  22. #include <string>
  23. #include <vector>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <time.h>
  28.  
  29. using namespace std;
  30.  
  31. #define sz(x) ((int) x.size())
  32.  
  33. typedef long double ld;
  34. typedef long long ll;
  35. typedef pair<int, int> pii;
  36.  
  37. const double pi = acos(-1);
  38. const double tau = 2*pi;
  39. const double epsilon = 1e-6;
  40.  
  41. const int MAX_STRING = 100100;
  42.  
  43. int T;
  44.  
  45. int N;
  46.  
  47. string S;
  48. int nxt[MAX_STRING];
  49. int prv[MAX_STRING];
  50.  
  51. vector<pii> curinterests;
  52. vector<pii> newinterests;
  53.  
  54. int main (int argc, const char * argv[])
  55. {
  56. ios_base::sync_with_stdio(0);
  57. cin.tie(NULL);
  58.  
  59. cin >> T;
  60.  
  61. for(int t = 0; t < T; t++)
  62. {
  63. cin >> N >> S;
  64.  
  65. for(int i = 0; i < N-1; i++)
  66. nxt[i] = i+1;
  67. nxt[N-1] = -1;
  68.  
  69. for(int i = 0; i < N; i++)
  70. prv[i] = i-1;
  71.  
  72. curinterests.clear();
  73. for(int i = 0; i < N-1; i++)
  74. if(S[i] == '4' && S[i+1] == '7')
  75. curinterests.push_back(pii(i,i));
  76.  
  77. ll P = 0;
  78. while(sz(curinterests) > 0)
  79. {
  80. newinterests.clear();
  81. for(int i = 0, nRemoved = 0; i < sz(curinterests); i++, ++nRemoved)
  82. {
  83. P += curinterests[i].second+1;
  84.  
  85. if(prv[curinterests[i].first] == -1 && nxt[nxt[curinterests[i].first]] != -1)
  86. {
  87. prv[nxt[nxt[curinterests[i].first]]] = prv[curinterests[i].first];
  88. continue;
  89. }
  90.  
  91. if(prv[curinterests[i].first] != -1 && nxt[nxt[curinterests[i].first]] == -1)
  92. {
  93. nxt[prv[curinterests[i].first]] = nxt[nxt[curinterests[i].first]];
  94. continue;
  95. }
  96.  
  97. if(prv[curinterests[i].first] == -1 && nxt[nxt[curinterests[i].first]] == -1) continue;
  98.  
  99. nxt[prv[curinterests[i].first]] = nxt[nxt[curinterests[i].first]];
  100. prv[nxt[nxt[curinterests[i].first]]] = prv[curinterests[i].first];
  101.  
  102. if(S[prv[curinterests[i].first]] == '4' && S[nxt[nxt[curinterests[i].first]]] == '7')
  103. newinterests.push_back(pii(prv[curinterests[i].first], curinterests[i].second-1-2*nRemoved));
  104. }
  105.  
  106. curinterests.clear();
  107. for(int i = 0; i < sz(newinterests); i++)
  108. curinterests.push_back(newinterests[i]);
  109. }
  110.  
  111. cout << P << '\n';
  112. }
  113.  
  114. return 0;
  115. }
Success #stdin #stdout 0s 4216KB
stdin
1
183
474777474747474444474477777474744474747477774744444474474747447474747474747474747774774744747474747474747474747474747474747474747474747444447777747474747474744747474747474444474774777
stdout
6857