Created
March 31, 2015 13:57
-
-
Save w181496/41f7f3bc597f4f6c700b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <cstring> | |
using namespace std; | |
typedef long long LL; | |
LL dp[200][200]; | |
string s; | |
LL f(LL i, LL j) | |
{ | |
if(i > j || i < 0 || j >= s.size()) return 0; | |
else if(dp[i][j] >= 0) return dp[i][j]; | |
else if(s[i] == s[j]) return dp[i][j] = f(i + 1, j) + f(i, j - 1) + 1; | |
else return dp[i][j] = f(i + 1, j) + f(i, j - 1) - f(i + 1, j - 1); | |
} | |
int main() | |
{ | |
ios_base::sync_with_stdio(0); | |
int t; | |
cin >> t; | |
while(t--) | |
{ | |
cin >> s; | |
for(int i = 0; i < s.size(); ++i) | |
{ | |
for(int j = 0; j < s.size(); ++j) | |
{ | |
if(i > j) dp[i][j] = 0; | |
else if(i == j) dp[i][j] = 1; | |
else dp[i][j] = -1; | |
} | |
} | |
cout << f(0, s.size() - 1) << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment