• Codeforces 159D:Palindrome pairs


    题意是给你一个字符串s,让你找到a,b,x,y(1<=a<=b<x<=y<|s|),且满足s[a,b],s[x,y]均是回文串。问存在多少种a,b,x,y的取值。

    做法是枚举回文串的中心,先预处理出以i起始的回文串的数目,和以i结束的回文串数目。然后再搞一下就行了。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 using namespace std;
     5 
     6 const int maxn = 2222;
     7 int pre[maxn], next[maxn];
     8 long long ans = 0;
     9 char s[maxn];
    10 
    11 int main() {
    12     scanf("%s", s);
    13     int len = strlen(s);
    14     for (int i = 0; i < len; i++) {
    15         for (int l = i, r = i; l >= 0 && r < len && s[l] == s[r]; l--, r++) {
    16             next[l]++; pre[r]++;
    17         }
    18         for (int l = i, r = i + 1; l >= 0 && r < len && s[l] == s[r]; l--, r++) {
    19             next[l]++; pre[r]++;
    20         }
    21     }
    22     for (int i = 1; i < len; i++)
    23         pre[i] += pre[i-1];
    24     for (int i = 0; i < len; i++)
    25         ans += (long long)pre[i] * next[i+1];
    26     printf("%lld\n", ans);
    27     return 0;
    28 }
  • 相关阅读:
    60个生僻成语汇总
    人民网 ***讲话
    组合排序
    工具
    网络之网络设备
    《TCP协议到TCP通讯各种异常现象和分析》的学习
    Console
    RTT学习之ulog
    RTT之MQTT学习笔记
    项目管理-禅道
  • 原文地址:https://www.cnblogs.com/phonism/p/3028149.html
Copyright © 2020-2023  润新知