Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<iostream>
- #include<vector>
- #include<cmath>
- #include<algorithm>
- #include<memory.h>
- #include<map>
- #include<set>
- #include<queue>
- #include<list>
- #include<sstream>
- #include<cstring>
- #include<numeric>
- using namespace std;
- const int N = 1e5 + 3;
- const int SZ = 5e7;
- char buf[SZ];
- int pos;
- void * operator new (size_t x){
- pos += x;
- if (pos > SZ)
- throw 42;
- return buf + pos - x;
- }
- void operator delete(void *x) { }
- char s[N];
- int main() {
- //freopen("input.txt", "r", stdin);
- int n;
- scanf("%d", &n);
- long long lastSuffix = 0;
- long long ans = 0;
- for (int j = 0; j < n; j++) {
- scanf("%s", s);
- long long maxBehind;
- long long c = 0;
- int len = strlen(s);
- vector<int> twos;
- for (int i = 0; i < len; i++) {
- if (s[i] == '2') {
- twos.push_back(i);
- }
- }
- if (twos.size() > 1) {
- for (int i = 1; i < twos.size() - 1; i++) {
- long long cur = twos[i] - twos[i - 1] + twos[i + 1] - twos[i] - 2;
- ans = max(cur, ans);
- }
- }
- int first = -1;
- int upTo = -1;
- for (int i = 0; i < len; i++) {
- if (s[i] == '2') {
- if(first == -1)
- first = i;
- else {
- upTo = i;
- break;
- }
- }
- }
- if (first == -1) {
- lastSuffix += len;
- ans = max(lastSuffix, ans);
- }
- else if (upTo == -1) {
- lastSuffix += len - 1;
- ans = max(lastSuffix, ans);
- }
- else {
- lastSuffix += upTo - 1;
- ans = max(lastSuffix, ans);
- first = -1;
- int last = -1;
- for (int i = len - 1; i >= 0; i--) {
- if (s[i] == '2') {
- if (first == -1) {
- first = i;
- last = i;
- }
- else {
- last = i;
- break;
- }
- }
- }
- lastSuffix = len - last - 2;
- ans = max(ans, lastSuffix);
- }
- }
- printf("%lld", max(ans, lastSuffix));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement