Isomorphic Inversion 哈希判断 Contest1818 - 2019年我能变强组队训练赛第十场

Isomorphic Inversion

题目描述
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.

在这里插入图片描述
题意:
给出一个序列,问这个序列中回文序列的个数是多少

分析:有没有一种方法去实现判断当前的序列是不是回文串呢???
那就可以用哈希判断了,其实说到底就是判断当前的值是不是相等的

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<string>
#include<cmath>
#include<cstring>
#include<set>
#include<queue>
#include<stack>
#include<map>
#define rep(i,a,b) for(int i=a;i<=b;i++)
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int N=1e6+10;
const int INF=0x3f3f3f3f;
char str[N];
ull base=81;
ull ha[N];
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
    scanf("%s",str+1);
    ha[0]=1;
    int len=strlen(str+1);
    for(int i=1;i<=len;i++) ha[i]=ha[i-1]*base;

    int i=1,j=len;
//    cout<<len<<endl;
    int ans=0;
    while(i<=j){
        ull res1=(str[i]-'0'),res2=(str[j]-'0');
        int t=j;
        while(res1!=res2){
            if(i+1>=j-1) break;
            i++;
            j--;
            res1=res1*base+(str[i]-'0');
            res2=(str[j]-'0')*ha[t-j]+res2;
        } 

        if(res1!=res2){
            ans++;
            break;
        }

        if(i!=j)
        ans+=2;
        if(i==j) ans++;
        i++;
        j--;
    }
    printf("%d\n",ans);
    return 0;
}


发布了229 篇原创文章 · 获赞 17 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/c___c18/article/details/100144266