fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. #define print(a) for(auto x : a) cout << x << " "; cout << endl
  6.  
  7.  
  8. const int M = 1000000007;
  9. const int N = 3e5+9;
  10. const int INF = 2e9+1;
  11. const int LINF = 2000000000000000001;
  12.  
  13. inline int power(int a, int b, int mod=M) {
  14. int x = 1;
  15. a %= mod;
  16. while (b) {
  17. if (b & 1) x = (x * a) % mod;
  18. a = (a * a) % mod;
  19. b >>= 1;
  20. }
  21. return x;
  22. }
  23.  
  24.  
  25. //_ ***************************** START Below *******************************
  26.  
  27.  
  28.  
  29.  
  30. string a;
  31.  
  32. int consistency1(int n){
  33.  
  34. vector<vector<bool>> dp(n, vector<bool>(n, false));
  35. vector<vector<int>> dpCt(n, vector<int>(n, 0));
  36.  
  37. for(int gap=0; gap<n; gap++){
  38. for(int i=0, j=gap; j<n; i++, j++){
  39. if(gap == 0){
  40. dp[i][j] = true;
  41. dpCt[i][j] = 1;
  42. }
  43. else if(gap == 1){
  44. if(a[i] == a[j]){
  45. dp[i][j] = true;
  46. dpCt[i][j] = 3;
  47. }
  48. else{
  49. dpCt[i][j] = 2;
  50. }
  51. }
  52. else{
  53. dpCt[i][j] = dpCt[i][j-1] + dpCt[i+1][j] - dpCt[i+1][j-1];
  54. if(a[i] == a[j] && dp[i+1][j-1]){
  55. dp[i][j] = true;
  56. dpCt[i][j]++;
  57. }
  58. }
  59. }
  60. }
  61.  
  62. int ans = 0;
  63. for(int i=0; i<=n-2; i++){
  64. int j = i;
  65. int x = 0;
  66. while(j>=0){
  67. if(dp[j][i]) x++;
  68. j--;
  69. }
  70. int y = dpCt[i+1][n-1];
  71.  
  72. ans += x*y;
  73. }
  74.  
  75. return ans;
  76. }
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. int consistency2(int n){
  85.  
  86. vector<vector<bool>> dp(n, vector<bool>(n, false));
  87. vector<vector<int>> dpCt(n, vector<int>(n, 0));
  88. vector<int> endsAt(n, 0);
  89.  
  90. for(int gap=0; gap<n; gap++){
  91. for(int i=0, j=gap; j<n; i++, j++){
  92. if(gap == 0){
  93. dp[i][j] = true;
  94. dpCt[i][j] = 1;
  95. endsAt[j] += 1;
  96. }
  97. else if(gap == 1){
  98. if(a[i] == a[j]){
  99. dp[i][j] = true;
  100. dpCt[i][j] = 3;
  101. endsAt[j] += 1;
  102. }
  103. else{
  104. dpCt[i][j] = 2;
  105. }
  106. }
  107. else{
  108. dpCt[i][j] = dpCt[i][j-1] + dpCt[i+1][j] - dpCt[i+1][j-1];
  109. if(a[i] == a[j] && dp[i+1][j-1]){
  110. dp[i][j] = true;
  111. dpCt[i][j]++;
  112. endsAt[j]++;
  113. }
  114. }
  115. }
  116. }
  117.  
  118.  
  119. // vector<int> endsAt(n, 0);
  120. // for(int j = 0; j < n; j++){
  121. // for(int i = 0; i <= j; i++){
  122. // if(dp[i][j]) endsAt[j]++;
  123. // }
  124. // }
  125.  
  126. int ans = 0;
  127. for(int i=0; i<=n-2; i++){
  128. int x = endsAt[i];
  129. int y = dpCt[i+1][n-1];
  130.  
  131. ans += x*y;
  132. }
  133.  
  134. return ans;
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150. int practice(int n){
  151.  
  152.  
  153. return 0;
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160. void solve() {
  161.  
  162. cin >> a;
  163. int n = a.size();
  164. cout << consistency1(n) << " " << consistency2(n) << endl;
  165.  
  166.  
  167. }
  168.  
  169.  
  170.  
  171.  
  172.  
  173. int32_t main() {
  174. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  175.  
  176. int t = 1;
  177. // cin >> t;
  178. while (t--) {
  179. solve();
  180. }
  181.  
  182. return 0;
  183. }
Success #stdin #stdout 0s 5316KB
stdin
pcbcqqcbcpsfdf
stdout
174 174