CF #541div2 E

题目本质:忽略串的变化,只记载26个字母的相关变化。

解决方法:

在上一次与本次的转移过程中,情况并不多,主要取决于本次串的首尾字母,若不是本次的首尾字母,会被置1;如果是的话,分情况接一下并更新。另外应该不会忘记的就是要拿本次串的最长串再更新一下。

复杂度最差时不是O(n2)吗?数据骗了。

  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cstdlib>
  5 #include <cmath>
  6 #include <ctime>
  7 #include <cctype>
  8 #include <climits>
  9 #include <iostream>
 10 #include <iomanip>
 11 #include <algorithm>
 12 #include <string>
 13 #include <sstream>
 14 #include <stack>
 15 #include <queue>
 16 #include <set>
 17 #include <map>
 18 #include <vector>
 19 #include <list>
 20 #include <fstream>
 21 #define ri readint()
 22 #define gc getchar()
 23 #define R(x) scanf("%d", &x)
 24 #define W(x) printf("%d
", x)
 25 #define init(a, b) memset(a, b, sizeof(a))
 26 #define rep(i, a, b) for (int i = a; i <= b; i++)
 27 #define irep(i, a, b) for (int i = a; i >= b; i--)
 28 #define ls p << 1
 29 #define rs p << 1 | 1
 30 using namespace std;
 31 
 32 typedef double db;
 33 typedef long long ll;
 34 typedef unsigned long long ull;
 35 typedef pair<int, int> P;
 36 const int inf = 0x3f3f3f3f;
 37 const ll INF = 1e18;
 38 
 39 inline int readint() {
 40     int x = 0, s = 1, c = gc;
 41     while (c <= 32)    c = gc;
 42     if (c == '-')    s = -1, c = gc;
 43     for (; isdigit(c); c = gc)
 44         x = x * 10 + c - 48;
 45     return x * s;
 46 }
 47 
 48 const int maxn = 1e5 + 5;
 49 
 50 vector<ll> getLength(string s) {
 51     vector<ll> v(26, 0);
 52     int len = 1, last = -1;
 53 
 54     rep(i, 0, s.length() - 1) {
 55         int now = s[i] - 'a';
 56         if (now == last) {
 57             len++;
 58         } else {
 59             last = now;
 60             len = 1;
 61         }
 62         v[now] = max(v[now], (ll)len);
 63     }
 64 
 65     return v;
 66 }
 67 
 68 int main() {
 69     ios_base::sync_with_stdio(false);
 70     cin.tie(0);
 71 
 72     int n;
 73     cin >> n;
 74     string s;
 75     cin >> s;
 76     vector<ll> Len = getLength(s);
 77 
 78     rep(i, 1, n - 1) {
 79         cin >> s;
 80         int prec = s[0] - 'a', sufc = s.back() - 'a';
 81         int prel = 1, sufl = 1;
 82         rep(j, 1, s.length() - 1) {
 83             if (s[j] - 'a' == prec)    prel++;
 84             else    break;
 85         }
 86         irep(j, s.length() - 2, 0) {
 87             if (s[j] - 'a' == sufc)    sufl++;
 88             else    break;
 89         }
 90 
 91         vector<ll> cur = getLength(s);
 92         if (prel == s.length()) {
 93             rep(j, 0, 25) {
 94                 if (j == prec) {
 95                     Len[j] += (Len[j] + 1) * prel;
 96                 } else {
 97                     Len[j] = min(Len[j], 1ll);
 98                 }
 99             }
100         } else {
101             rep(j, 0, 25) {
102                 if (Len[j])
103                     Len[j] = (prec == j) * prel + (sufc == j) * sufl + 1;
104                 Len[j] = max(Len[j], cur[j]);
105             }
106         }
107 
108         rep(j, 0, 25)    Len[j] = min(Len[j], (ll)1e9);
109     }
110 
111     ll ans = -1;
112     rep(i, 0, 25)    ans = max(ans, Len[i]);
113     cout << ans << endl;
114     return 0;
115 }
原文地址:https://www.cnblogs.com/AlphaWA/p/10439277.html