Advertisement
Dmaxiya

更小的数 参考代码

Apr 4th, 2025
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 5000 + 100;
  6. int n, ans;
  7. char str[maxn];
  8. int dp[maxn][maxn];
  9.  
  10. int main() {
  11. #ifdef ExRoc
  12.     freopen("test.txt", "r", stdin);
  13. #endif // ExRoc
  14.     ios::sync_with_stdio(false);
  15.  
  16.     cin >> (str + 1);
  17.     n = strlen(str + 1);
  18.     for (int len = 1; len <= n; ++len) {
  19.         for (int l = 1; l + len - 1 <= n; ++l) {
  20.             int r = l + len - 1;
  21.             if (str[r] < str[l]) {
  22.                 dp[l][r] = 1;
  23.             } else if (str[r] > str[l]) {
  24.                 dp[l][r] = -1;
  25.             } else {
  26.                 dp[l][r] = dp[l + 1][r - 1];
  27.             }
  28.             if (dp[l][r] == 1) {
  29.                 ++ans;
  30.             }
  31.         }
  32.     }
  33.     cout << ans << endl;
  34.  
  35.     return 0;
  36. }
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement