题解:
我门可以用枚举26个字符的情况,然后取大。
现在我们先讨论a的情况:
那么当 a 的个数为0的时候, a 的个数只有在1种情况下才会被更新:
新加入的串中有连续的a, 我们取中间最大的。
然后当 a 的个数不为1的时候, a的个数更新方式有2种:
1. 新加入的串全为a, 那么就会把全部的串连起来,答案为 tmp + len * (tmp + 1)。
2. 新加入的串不全为a, 那么答案在以下取大:
(1) 新加入的串中最长的连续a (2) 新加入的串.back + a + 新加入的串.front。
代码:
/* code by: zstu wxk time: 2019/02/24 */ #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 1e5 + 100; int n; string str[N]; bool check(char ch, int x){ for(int i = 0; i < str[x].size(); ++i){ if(ch != str[x][i]) return false; } return true; } int cal(char ch, int x){ int ret = 0, tmp = 0; for(int i = 0; i < str[x].size(); ++i){ if(ch == str[x][i]) tmp++; else tmp = 0; ret = max(ret, tmp); } return ret; } int Front(char ch, int x){ int ret = 0; for(int i = 0; i < str[x].size(); ++i){ if(ch == str[x][i]) ret++; else return ret; } } int Back(char ch, int x){ int ret = 0; for(int i = str[x].size() - 1; i >= 0; --i){ if(ch == str[x][i]) ret++; else return ret; } } void Ac(){ for(int i = 1; i <= n; ++i) cin >> str[i]; LL ans = 0; for(int i = 0; i < 26; ++i){ LL tmp = 0; for(int j = 1; j <= n; ++j){ if(tmp == 0) { tmp = cal('a'+i, j); } else { if(check('a'+i,j)){ tmp = tmp + str[j].size() * (tmp+1); } else { tmp = max(1+Back('a'+i,j)+Front('a'+i,j), cal('a'+i,j)); } } } ans = max(ans, tmp); } cout << ans << endl; } int main(){ while(~scanf("%d", &n)){ Ac(); } return 0; }