Advertisement
Guest User

Untitled

a guest
Jan 17th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<iostream>
  3. #include<vector>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<memory.h>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<list>
  11. #include<sstream>
  12. #include<cstring>
  13. #include<numeric>
  14. using namespace std;
  15.  
  16. const int N = 1e5 + 3;
  17. const int SZ = 5e7;
  18. char buf[SZ];
  19. int pos;
  20. void * operator new (size_t x){
  21.     pos += x;
  22. if (pos > SZ)
  23. throw 42;
  24. return buf + pos - x;
  25. }
  26. void operator delete(void *x) { }
  27. char s[N];
  28.  
  29. int main() {
  30.     //freopen("input.txt", "r", stdin);
  31.     int n;
  32.     scanf("%d", &n);
  33.     long long lastSuffix = 0;
  34.     long long ans = 0;
  35.     for (int j = 0; j < n; j++) {
  36.         scanf("%s", s);
  37.  
  38.         long long maxBehind;
  39.         long long c = 0;
  40.  
  41.         int len = strlen(s);
  42.        
  43.        
  44.         vector<int> twos;
  45.         for (int i = 0; i < len; i++) {
  46.             if (s[i] == '2') {
  47.                 twos.push_back(i);
  48.             }
  49.         }
  50.  
  51.         if (twos.size() > 1) {
  52.             for (int i = 1; i < twos.size() - 1; i++) {
  53.                 long long cur = twos[i] - twos[i - 1] + twos[i + 1] - twos[i] - 2;
  54.                 ans = max(cur, ans);
  55.             }
  56.         }
  57.  
  58.  
  59.         int first = -1;
  60.         int upTo = -1;
  61.         for (int i = 0; i < len; i++) {
  62.             if (s[i] == '2') {
  63.                 if(first == -1)
  64.                     first = i;
  65.                 else {
  66.                     upTo = i;
  67.                     break;
  68.                 }
  69.             }
  70.         }
  71.         if (first == -1) {
  72.             lastSuffix += len;
  73.             ans = max(lastSuffix, ans);
  74.         }
  75.         else if (upTo == -1) {
  76.             lastSuffix += len - 1;
  77.             ans = max(lastSuffix, ans);
  78.         }
  79.         else {
  80.             lastSuffix += upTo - 1;
  81.             ans = max(lastSuffix, ans);
  82.             first = -1;
  83.             int last = -1;
  84.             for (int i = len - 1; i >= 0; i--) {
  85.                 if (s[i] == '2') {
  86.                     if (first == -1) {
  87.                         first = i;
  88.                         last = i;
  89.                     }
  90.                     else {
  91.                         last = i;
  92.                         break;
  93.                     }
  94.                 }
  95.             }
  96.             lastSuffix = len - last - 2;
  97.             ans = max(ans, lastSuffix);
  98.         }
  99.     }
  100.     printf("%lld", max(ans, lastSuffix));
  101.  
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement