您的位置:首页 > 大数据 > 人工智能

CodeForces-159D Palindrome pairs DP

2017-02-22 19:09 302 查看
也可以用Manacher算法做 但是我觉得有点麻烦..而且没搞出来= =

#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <algorithm>
#include <functional>
#include <vector>
#include <iomanip>
#include <cmath>
#include <iostream>
#include <sstream>
#include <stack>
#include <set>
#include <bitset>
using namespace std;

const int MAX = 2005;

int F[MAX], G[MAX];
string Str;

int main()
{
while (cin >> Str)
{
size_t len = Str.length();
memset(F, 0, sizeof(F));
memset(G, 0, sizeof(G));
for (int i = 0; i < len; i++)
{
for (int l = i, r = i; l >= 0 && r < len && Str[l] == Str[r]; l--, r++)
F[l]++, G[r]++;
for (int l = i, r = i + 1; l >= 0 && r < len && Str[l] == Str[r]; l--, r++)
F[l]++, G[r]++;
}
for (int i = 1; i < len; i++)
G[i] += G[i - 1];
long long Ans = 0;
for (int i = 1; i < len; i++)
Ans += G[i - 1] * F[i];
cout << Ans << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: