码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-09-03 11:42:44      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:class   内存   ima   +=   orm   out   string   字符串哈希   for   

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\n",ans);
46     return 0;
47 }

 

 

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

标签:class   内存   ima   +=   orm   out   string   字符串哈希   for   

原文地址:https://www.cnblogs.com/CharlieWade/p/11451753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!