Isomorphic Inversion

时间:2020-04-16
本文章向大家介绍Isomorphic Inversion,主要包括Isomorphic Inversion使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Let s be a given string of up to 10^6106 digits. Find the maximal kk for which it is possible to partition ss into kk consecutive contiguous substrings, such that the kk parts form a palindrome. More precisely, we say that strings s_0, s_1, . . . , s_{k-1}s0,s1,...,sk1 form a palindrome if s_i = s_{k-1-i}si=sk1i for all 0\leq i <k0i<k.

In the first sample case, we can split the string 652526 into 4 parts as 6|52|52|6, and these parts together form a palindrome. It turns out that it is impossible to split this input into more than 4 parts while still making sure the parts form a palindrome.

Input:

  • A nonempty string of up to 10^6106 digits.

Output:

  • Print the maximal value of kk on a single line.

样例输入1

652526

样例输出1

4

样例输入2

12121131221

样例输出2

7

样例输入3

132594414896459441321

样例输出3

9

滚动哈希
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)        for(int  i=a;i<=b;i++)
#define dec(i,a,b)        for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1e9 + 7;
const int N = 1005;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}

ll p = 131;

int main() 
{
    string s;
    cin >> s;
    int n = s.size();
    vector<ll> pows(s.size() + 1), hash(s.size() + 1);
    hash[0] = 0;
    pows[0] = 1;
    for (int i = 0; i < n; ++i) 
    {
        pows[i + 1] = pows[i] * p % mod;
        hash[i + 1] = (hash[i] + pows[i] * s[i]) % mod;
    }

    int ans = 0;
    int l = 0, r = n;
    for (int i = 1; i <= n - i; ++i) 
    {
        if ((mod + hash[i] - hash[l]) * pows[r - i] % mod == (mod + hash[r] - hash[n - i]) % mod) 
        {
            ans += 2;
            l = i;
            r = n - i;
        }
    }
    if (l < r) ++ans;
    cout << ans << endl;
    return 0;
}

原文地址:https://www.cnblogs.com/dealer/p/12715281.html