zoukankan      html  css  js  c++  java
  • B

    http://agc019.contest.atcoder.jp/tasks/agc019_b

    一开始的做法是,

    用总数减去回文子串数目,因为回文子串怎么翻转都不影响答案。

    然后,如果翻转afucka,那么和翻转fuck,得到的串是一样的。

    但是如果是先是用total - 回文子串数目,再减去afucka这样的,一头一尾相同,但是又不是回文串的字符串,复杂度要O(n^2)

    考虑到回文串也是一头一尾相同的,那么相当于翻转一头一尾不相同的字符串才能得到新的贡献

    相当于total - (一头一尾相同的)

    #include <bits/stdc++.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    const int maxn = 200000 * 2 + 20;
    char str[maxn];
    int num[maxn];
    void work() {
        scanf("%s", str + 1);
        int lenstr = strlen(str + 1);
        LL ans = 1LL * lenstr * (lenstr - 1) / 2 + 1;
        for (int i = 1; i <= lenstr; ++i) {
            num[str[i]]++;
        }
        for (int i = 'a'; i <= 'z'; ++i) {
            ans -= 1LL * num[i] * (num[i] - 1) / 2;
        }
        cout << ans << endl;
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        work();
        return 0;
    }
    View Code
  • 相关阅读:
    Java学习总结
    John 尼姆博弈
    博弈知识汇总(转)
    坏习纠正
    QDUOJ 河老师的新年礼物(尺取法)
    HDU
    QDUOJ ycb的ACM进阶之路 二进制多重背包
    HDU
    HDU
    POJ
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/7441611.html
Copyright © 2011-2022 走看看