程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> BZOJ 3916 Baltic 2014 friends Hash

BZOJ 3916 Baltic 2014 friends Hash

編輯:關於C++

題目大意

給出一個字符串,這個字符串是由兩個相同的字符串連接之後再加一個字母的到的,求原串。

思路

枚舉加的字母是哪一個,之後分情況討論,根據hash值來判定是否符合題目要求。注意判重。

CODE

#define _CRT_SECURE_NO_WARNINGS

#include 
#include 
#include 
#include 
#define MAX 2000010
#define BASE 23333
using 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;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved