lightoj 1025 - The Specials Menu - dblank

lightoj 1025 - The Specials Menu

Feuzem is an unemployed computer scientist who spends his days working at odd-jobs. While on the job he always manages to find algorithmic problems within mundane aspects of everyday life.

Today, while writing down the specials menu at the restaurant he's working at, he felt irritated by the lack of palindromes (strings which stay the same when reversed) on the menu. Feuzem is a big fan of palindromic problems, and started thinking about the number of ways he could remove letters from a particular word so that it would become a palindrome.

Two ways that differ due to order of removing letters are considered the same. And it can also be the case that no letters have to be removed to form a palindrome.
Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains a single word W (1 ≤ length(W) ≤ 60).
For each case, print the case number and the total number of ways to remove letters from W such that it becomes a palindrome.

<h1>Sample Input</h1> <h1>Output for Sample Input</h1>
3 SALADS PASTA YUMMY Case 1: 15 Case 2: 8 Case 3: 11


思路:区间dp,用dpi表示从s[i]到是s[j]的方案数。对于一个dpi我们可以删除第i个和第j个使它变成回文串,但是这个地方会多出一个两个都删除的情况,于是要减掉,这个s[i] != s[j]的时候,如果相等的话,我们可以保留s[i]和s[j],还可以把s[i]和s[j]中间都删除.

using namespace std;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f, N = 60 + 10, Mod = 1000000007;
typedef long long ll;
int pathN;
ll dpN;
char s[N];
int main()
    int t;
    for(int cas = 1; cas<=t; cas++)
        scanf("%s", s);
        memset(dp, 0, sizeof(dp));
        memset(path, 0, sizeof(path));
        int len = strlen(s);
        for(int i = 0; i<len; i++)
            dpi = 1;
        for(int i = len-1; i>=0; i--)
            for(int j = i+1; j<len; j++)
                if(s[i] == s[j])
                    dpi += dpi+1 + dpi  + 1;
                else dpi += dpi+1 + dpi - dpi+1;
        ll ans = dp0;
        printf("Case %d: %lldn",cas,  ans);
    return 0;