fork download
  1. // ~~ icebear ~~
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. #define int long long
  5. typedef long long ll;
  6. typedef pair<int, int> ii;
  7. typedef pair<ii, int> iii;
  8.  
  9. #define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
  10. #define FORR(i,a,b) for(int i=(a); i>=(b); --i)
  11. #define rep(i, n) for(int i=0; i<(n); ++i)
  12. #define red(i, n) for(int i=(n)-1; i>=0; --i)
  13. #define mp make_pair
  14. #define pb push_back
  15. #define fi first
  16. #define se second
  17. #define all(x) x.begin(), x.end()
  18. #define task "icebearat"
  19.  
  20. const int MOD = 1e9 + 7;
  21. const int inf = 1e9 + 27092008;
  22. const ll LLinf = 1e18 + 27092008;
  23. const int N = 5000 + 5;
  24. int n, m;
  25. string s, t;
  26. int prefA[N][26], prefB[N][26], lastA[N], lastB[N];
  27. int best[N][N], cnt[N];
  28. struct {
  29. int ch, cnt;
  30. } fA[N], fB[N];
  31. int f[N][N], id[26];
  32.  
  33. int sumA(int l, int r, int c) { return (prefA[r][c] - prefA[l - 1][c]); }
  34. int sumB(int l, int r, int c) { return (prefB[r][c] - prefB[l - 1][c]); }
  35.  
  36. void solve() {
  37. cin >> s >> t;
  38. for(char c : s)
  39. if('a' <= c && c <= 'z') fA[++n].ch = c - 'a';
  40. else fA[n].cnt = fA[n].cnt * 10 + (c - '0');
  41.  
  42. for(char c : t)
  43. if ('a' <= c && c <= 'z') fB[++m].ch = c -'a';
  44. else fB[m].cnt = fB[m].cnt * 10 + (c - '0');
  45.  
  46. FOR(i, 1, n) {
  47. lastA[i] = id[fA[i].ch];
  48. id[fA[i].ch] = i;
  49. }
  50. memset(id, 0, sizeof id);
  51. FOR(i, 1, m) {
  52. lastB[i] = id[fB[i].ch];
  53. id[fB[i].ch] = i;
  54. }
  55.  
  56. FOR(i, 1, n) FOR(j, 0, 25) prefA[i][j] = prefA[i - 1][j] + (fA[i].ch == j) * fA[i].cnt;
  57. FOR(i, 1, m) FOR(j, 0, 25) prefB[i][j] = prefB[i - 1][j] + (fB[i].ch == j) * fB[i].cnt;
  58.  
  59. FOR(i, 1, n) FOR(j, 1, m) {
  60. f[i][j] = max(f[i - 1][j], f[i][j - 1]);
  61.  
  62. if (fA[i].ch == fB[j].ch) {
  63. int x = i, y = j, c = fA[i].ch;
  64. bool IloveAT = true;
  65. while(IloveAT) {
  66. if (sumA(x, i, c) > sumB(y, j, c)) {
  67. if (x && y)
  68. f[i][j] = max(f[i][j], f[x - 1][y - 1] + sumB(y, j, c));
  69. y = lastB[y];
  70. if (y == 0) break;
  71. } else {
  72. if (x && y)
  73. f[i][j] = max(f[i][j], f[x - 1][y - 1] + sumA(x, i, c));
  74. x = lastA[x];
  75. if (x == 0) break;
  76. }
  77. }
  78. }
  79. }
  80. cout << f[n][m] << endl;
  81.  
  82. memset(f, 0, sizeof f);
  83. FOR (i, 1, n) cnt[i] = cnt[i-1] + fA[i].cnt;
  84. int ans = 0;
  85. FOR (i, 1, n) FOR (j, 1, m){
  86. if (fA[i].ch == fB[j].ch)
  87. f[i][j] = min(fA[i].cnt, fB[j].cnt);
  88. if (fA[i].ch == fB[j].ch && fA[i].cnt == fB[j].cnt)
  89. best[i][j] = best[i-1][j-1] + 1;
  90. }
  91. FOR (i, 1, n) FOR (j, 1, m){
  92. ans = max(ans, cnt[i] - cnt[i-best[i][j]] + f[i+1][j+1] + f[i-best[i][j]][j-best[i][j]]);
  93. }
  94. cout << ans;
  95.  
  96. }
  97.  
  98. signed main() {
  99. ios_base::sync_with_stdio(0);
  100. cin.tie(0); cout.tie(0);
  101. if (fopen(task".inp", "r")){
  102. freopen(task".inp", "r", stdin);
  103. freopen(task".out", "w", stdout);
  104. }
  105. int tc = 1;
  106. // cin >> tc;
  107. while(tc--) solve();
  108. return 0;
  109. }
  110.  
Success #stdin #stdout 0.03s 201384KB
stdin
Standard input is empty
stdout
0
0