-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathodd palindrome.cpp
73 lines (73 loc) · 2.25 KB
/
odd palindrome.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<bits/stdc++.h>
using namespace std;
long long mod = 1000000007 ;
typedef long long ll;
/// find the number of palindromic subsequences around each center.
int main()
{
string s;
cin >> s;
ll n = s.length();
ll dp[n][n];
memset(dp,0,sizeof dp);
for(ll len = n-1 ; len >=0 ; --len){
for(ll i = 0 ; i+len<n ; ++i){
ll j = i+len;
if(i == 0 and j==n-1){
if(s[i] == s[j]) dp[i][j]= 2 ;
else dp[i][j] = 1 ;
}
else{
/// if characters are equal
if(s[i]==s[j]){
if(i-1>=0){
dp[i][j] += dp[i-1][j];
dp[i][j] = dp[i][j] % mod ;
}
if(j+1<=n-1){
dp[i][j] += dp[i][j+1];
dp[i][j] = dp[i][j] % mod ;
}
if(i-1 < 0 or j+1 >= n){
dp[i][j]+= 1;
dp[i][j] = dp[i][j] % mod ;
}
}
/// if not equal
else if(s[i]!=s[j]){
ll sum = 0 ;
if(i -1 >=0){
sum = sum + dp[i-1][j];
sum = sum % mod ;
}
if(j+1<=n-1){
sum = sum + dp[i][j+1] ;
sum = sum % mod ;
}
if(i-1>=0 and j+1<=n-1){
sum = sum - dp[i-1][j+1];
sum = sum % mod ;
}
dp[i][j] += sum % mod ;
dp[i][j] = dp[i][j] % mod;
}
}
}
}
/// abaa
for(ll i=0;i<=n-1;++i){
if(i==0 or i == n-1) cout << 1 << " ";
else {
ll res = 0 ;
if(dp[i-1][i+1] > 0 ){
ll res = dp[i-1][i+1] % mod ;
cout << res << " " ;
}
else if(dp[i-1][i+1] <0 ){
ll res = dp[i-1][i+1] + mod ;
res = res % mod ;
cout << res << " " ;
}
}
}
}