题意:
给定一系列字符串,每次都是后一个字符串和前面的融合,这个融合操作就是原来的串分成独立的,然后把新串插入到这些空格中。问最后,最长的相同连续的长度。
思路:
这道题可以贪心的来,我们压缩状态,记录串中每个字母对应最长的长度。然后分类讨论处理就行了。
#include <algorithm> #include <iterator> #include <iostream> #include <cstring> #include <cstdlib> #include <iomanip> #include <bitset> #include <cctype> #include <cstdio> #include <string> #include <vector> #include <stack> #include <cmath> #include <queue> #include <list> #include <map> #include <set> #include <cassert> /* ⊂_ヽ \\ Λ_Λ 来了老弟 \('ㅅ') > ⌒ヽ / へ\ / / \\ レ ノ ヽ_つ / / / /| ( (ヽ | |、\ | 丿 \ ⌒) | | ) / 'ノ ) Lノ */ using namespace std; #define lson (l , mid , rt << 1) #define rson (mid + 1 , r , rt << 1 | 1) #define debug(x) cerr << #x << " = " << x << " "; #define pb push_back #define pq priority_queue typedef long long ll; typedef unsigned long long ull; //typedef __int128 bll; typedef pair<ll ,ll > pll; typedef pair<int ,int > pii; typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q //priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q #define fi first #define se second //#define endl ' ' #define boost ios::sync_with_stdio(false);cin.tie(0) #define rep(a, b, c) for(int a = (b); a <= (c); ++ a) #define max3(a,b,c) max(max(a,b), c); #define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; const double PI=acos(-1.0); const double PHI=0.61803399; //黄金分割点 const double tPHI=0.38196601; template<typename T> inline T read(T&x){ x=0;int f=0;char ch=getchar(); while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar(); while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x=f?-x:x; } inline void cmax(int &x,int y){if(x<y)x=y;} inline void cmax(ll &x,ll y){if(x<y)x=y;} inline void cmin(int &x,int y){if(x>y)x=y;} inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const ll big = 1e9+3; ll dp[30],tmp[30]; string str; int main(){ int n; scanf("%d", &n); ll ans = 0; rep(cc, 1, n){ cin>>str; int len = str.length(); int flag = 1; for(int i=0; i<len; i++) if(str[i] != str[0]) flag = 0; if(flag) { int id = (str[0] - 'a'); rep(i, 0, 25) { if(i == id) continue; if(dp[i]) dp[i] = 1; } if(dp[id]){ ll t = min(big, 1ll*(dp[id] + 1) * len + dp[id]); dp[id] = max(dp[id], t); } dp[id] = max(1ll*len, dp[id]); } else { rep(i, 0, 25){ if(dp[i]) dp[i] = 1; tmp[i] = 0; } ll e = 1;char la = str[0]; str+="A"; rep(i, 1, len){ if(str[i] != la){ int id = (int)(la - 'a'); tmp[id] = max(tmp[id], e); // debug(e); la = str[i]; e = 1; } else e++; } ll c1 = 0,c2 = 0; for(int i=0; i<len && str[i] == str[0]; i++) c1++; for(int i=len-1; i>=0 && str[i] == str[len-1];i--) c2++; if(str[0] == str[len-1]) { int id = (int)(str[0] - 'a'); if(dp[id]) dp[id] = min(big, max(dp[id], 1ll + c1 + c2)); } else { int id = (int)(str[0] - 'a'); if(dp[id]) dp[id] = min(big, max(dp[id], 1ll + c1)); id = (int) (str[len-1] - 'a'); if(dp[id]) dp[id] = min(big, max(dp[id], 1ll + c2)); } rep(i, 0, 25) { dp[i] = max(dp[i], tmp[i]); } } } rep(i, 0, 25) ans = max(ans, dp[i]); printf("%lld ", ans); return 0; }