求大佬给一组hack供调试
  • 板块学术版
  • 楼主qpdk777
  • 当前回复3
  • 已保存回复3
  • 发布时间2020/4/29 14:01
  • 上次更新2023/11/7 03:41:26
查看原帖
求大佬给一组hack供调试
261932
qpdk777楼主2020/4/29 14:01

原题:一本通1459

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define ull unsigned long long
#define MAX 2000010
using namespace std;

int n,flag;
string s;
ull hash1,hash2,hash3;
ull hashn[MAX],pown[MAX];
const int d = 19260817;

int main(){
	ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
	cin>>n;
	if(n&1==0){cout<<"NOT POSSIBLE"<<endl;return 0;}
	cin>>s;
	s.insert(0," ");
	pown[0] = 1;
	for(int i = 1; i <= n; ++i){
		pown[i] = pown[i-1]*d;
		hashn[i] = hashn[i-1]*d+s[i]-'A'+1;
	}
	int mid = n/2+1;
	
	for(int i = 1; i < mid; ++i){
		hash1 = hashn[i-1];
		hash2 = hashn[mid]-hashn[i]*pown[mid-i];
		hash3 = hashn[n]-hashn[mid]*pown[n-mid];
		if(hash1*pown[mid-i]+hash2 == hash3){
			if(flag){
				cout<<"NOT UNIQUE"<<endl;
				return 0;
			}
			flag = i;
		}
	}
	
	hash1 = hashn[mid-1];
	hash3 = hashn[n]-hashn[mid]*pown[n-mid];
	if(hash1 == hash3){
		if(flag){
			cout<<"NOT UNIQUE"<<endl;
			return 0;
		}
		flag = mid;
	}
	
	for(int i = mid+1; i <= n; ++i){
		hash1 = hashn[mid-1];
		hash2 = hashn[i-1]-hashn[mid-1]*pown[i-mid];
		hash3 = hashn[n]-hashn[i]*pown[n-i];
		if(hash1 == hash2*pown[n-i]+hash3){
			if(flag){
				cout<<"NOT UNIQUE"<<endl;
				return 0;
			}
		flag = i;
		}
	}
	
	if(flag == 0)
		cout<<"NOT POSSIBLE"<<endl;
	else{
		s.erase(flag,flag+1);
		cout<<s.substr(1,mid+1)<<endl;
	}
	
	
	return 0;
}
2020/4/29 14:01
加载中...