• BAPC 2018 Preliminaries-Isomorphic Inversion(字符串哈希)


    Isomorphic Inversion

    时间限制: 1 Sec  内存限制: 128 MB

    题目描述

    Let s be a given string of up to 106 digits. Find the maximal k for which it is possible to partition s into k consecutive contiguous substrings, such that the k parts form a palindrome.
    More precisely, we say that strings s0, s1, . . . , sk−1 form a palindrome if si = sk−1−i for all 0 ≤ i < 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.

    输入

    A nonempty string of up to 106 digits.

    输出

    Print the maximal value of k on a single line.

    样例输入

    652526
    

    样例输出

    4


    题意:给一串数字串,求问最多能分成多少个区域使得区域回文。
     1 #include<bits/stdc++.h>
     2 #pragma GCC optimize(3)
     3 using namespace std;
     4 typedef long long ll;
     5 const int maxn=1e6+7;
     6 const ll prime=97;
     7 ll Ha[maxn];
     8 char str[maxn];
     9 void init()
    10 {
    11     Ha[0]=1;
    12     for(int i=1; i<=maxn-5; ++i)
    13     {
    14         Ha[i]=Ha[i-1]*prime;
    15     }
    16 }
    17 int main()
    18 {
    19     init();
    20     int ans=0;
    21     scanf("%s",str+1);
    22     int len=strlen(str+1);
    23     int l=1,r=len;
    24     while(l<=r)
    25     {
    26         int start=r;
    27         ll ldata=ll(str[l]-'0'),rdata=ll(str[r]-'0');
    28         while(ldata!=rdata&&(l<r))
    29         {
    30             ++l;
    31             --r;
    32             ldata=ll(str[l]-'0')+ldata*prime;
    33             rdata+=ll(str[r]-'0')*Ha[start-r];
    34         }
    35         if(ldata!=rdata)
    36         {
    37             ++ans;
    38             break;
    39         }
    40         if(l!=r)ans+=2;
    41         else ++ans;
    42         ++l;
    43         --r;
    44     }
    45     printf("%d
    ",ans);
    46     return 0;
    47 }
     
  • 相关阅读:
    动态投影
    我的比较差的初级的研究成果
    我最近的研究成果(IGeometry.Project and IGeometry.SpatialReference)
    mysql中的数据类型以及常见约束
    面向对象——多态
    java基础
    java中的异常(3)
    mysql中的数据类型
    面向对象——继承
    java中的异常(2)
  • 原文地址:https://www.cnblogs.com/CharlieWade/p/11451753.html
Copyright © 2020-2023  润新知