BZOJ 3916 Baltic 2014 friends Hash

Source: Internet
Author: User

BZOJ 3916 Baltic 2014 friends Hash
Theme

Returns a string that is connected by two identical strings and followed by a letter. Evaluate the original string.

Ideas

Which of the following is the name of the letter added to the enumeration? Based on the hash value, we will discuss the problem in detail. Pay attention to heavy judgment.

CODE
#define _CRT_SECURE_NO_WARNINGS#include 
  
   #include 
   
    #include 
    
     #include #define MAX 2000010#define BASE 23333using namespace std;int length, mid;char s[MAX];unsigned long long _hash[MAX];unsigned long long power[MAX];int cnt;char ans[MAX], now[MAX];unsigned long long h_ans, h_now;inline unsigned long long GetHash(int l, int r){    return _hash[r] - _hash[l - 1] * power[r - l + 1];}inline bool Judge(int dismiss){    static unsigned long long front, tail;    if(dismiss < mid) {        front = GetHash(1, dismiss - 1) * power[mid - dismiss] + GetHash(dismiss + 1, mid);        tail = GetHash(mid + 1, length);    }    else if(dismiss == mid) {        front = GetHash(1, mid - 1);        tail = GetHash(mid + 1, length);    }    else {        front = GetHash(1, mid - 1);        tail = GetHash(mid, dismiss - 1) * power[length - dismiss] + GetHash(dismiss + 1, length);    }    if(front == tail) {        if(h_ans == front)  return false;        h_ans = front;        int top = 0;        if(dismiss <= mid)            for(int i = mid + 1; i <= length; ++i)                ans[++top] = s[i];        else            for(int i = 1; i < mid; ++i)                ans[i] = s[i];        return true;    }    return false;}int main(){    cin >> length;    if(!(length&1)) {        puts("NOT POSSIBLE");        return 0;    }    mid = (length >> 1) + 1;    scanf("%s", s + 1);    power[0] = 1;    for(int i = 1; i <= length; ++i)        power[i] = power[i - 1] * BASE;    for(int i = 1; i <= length; ++i)        _hash[i] = _hash[i - 1] * BASE + s[i];    for(int i = 1; i <= length; ++i) {        cnt += Judge(i);        if(cnt > 1) break;    }    if(!cnt)        puts("NOT POSSIBLE");    if(cnt == 1)        puts(ans + 1);    if(cnt > 1)        puts("NOT UNIQUE");    return 0;}
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.