-
Notifications
You must be signed in to change notification settings - Fork 1
/
Distinct palindromic substrings.cpp
52 lines (47 loc) · 1.16 KB
/
Distinct palindromic substrings.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
#include<bits/stdc++.h>
using namespace std;
int distinctPalindrome(string);
int main(){
int t;
string str;
cin >> t;
while(t--){
cin >> str;
cout << distinctPalindrome(str) << endl;
}
return 0;
}
int distinctPalindrome(string str){
int l = str.length();
bool dp[l][l];
unordered_set<string> us;
for(int i = 0; i < l; i++){
dp[i][i] = true;
string temp(1, str[i]);
us.insert(temp);
}
for(int cl = 2; cl <= l; cl++){
for(int i = 0; i < l - cl + 1; i++){
int j = i + cl - 1;
if(cl == 2){
if(str[i] == str[j]){
dp[i][j] = true;
string temp = str.substr(i, j - i + 1);
us.insert(temp);
}
else
dp[i][j] = false;
}
else{
if(str[i] == str[j] && dp[i + 1][j - 1]){
dp[i][j] = true;
string temp = str.substr(i, j - i + 1);
us.insert(temp);
}
else
dp[i][j] = false;
}
}
}
return us.size();
}