随笔 - 141,  文章 - 0,  评论 - 0,  阅读 - 1402

Description

有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到U.现在你得到了U,请你找出S.

 

Input

第一行一个数N,表示U的长度.

第二行一个字符串U,保证U由大写字母组成

 

Output

输出一行,若S不存在,输出"NOT POSSIBLE".若S不唯一,输出"NOT UNIQUE".否则输出S.

 

Sample Input

Sample Input1:
7
ABXCABC


Sample Input2:

6
ABCDEF

Sample Input3:

9
ABABABABA

Sample Output

Sample Output1:

ABC

Sample Output2:

NOT POSSIBLE

Sample Output3:

NOT UNIQUE

HINT

 

对于100%的数据 2<=N<=2000001

这道题直接枚举断点,hash判断即可,注意这里的不同指本质不同,下面是程序:

#include<stdio.h>
#include<iostream>
using namespace std;
const int N=2000005;
unsigned int base[N]={1},hash[N];
int n,m;
char c[N];
void into(){
	int i;
	scanf("%d",&n);
	m=n>>1;
	for(i=1;i<=n;i++){
		c[i]=getchar();
		while(c[i]<'A'||c[i]>'Z'){
			c[i]=getchar();
		}
		hash[i]=hash[i-1]*29+c[i]-'A'+1;
		base[i]=base[i-1]*29;
	}
}
unsigned int geth(int l,int r){
	return hash[r]-hash[l-1]*base[r-l+1];
}
bool checkh(int k){
	if(k==m+1){
		return geth(1,m)==geth(m+2,n);
	}
	if(k<=m){
		return geth(1,k-1)*base[m-k+1]+geth(k+1,m+1)==geth(m+2,n);
	}
	if(k>m){
		return geth(1,m)==geth(m+1,k-1)*base[n-k]+geth(k+1,n);
	}
}
void work(){
	if(!(n%2)){
		printf("NOT POSSIBLE\n");
		return;
	}
	int i,cnt=0,s;
	unsigned int ans,tp;
	for(i=1;i<=n;i++){
		if(checkh(i)){
			if(!cnt){
				cnt=1;
				s=i;
				if(i<=m){
					ans=geth(m+2,n);
				}
				else{
					ans=geth(1,m);
				}
			}
			else{
				if(i<=m){
					tp=geth(m+2,n);
				}
				else{
					tp=geth(1,m);
				}
				if(tp!=ans){
					cnt++;
					break;
				}
			}
		}
	}
	if(cnt==0){
		printf("NOT POSSIBLE\n");
		return;
	}
	if(cnt>1){
		printf("NOT UNIQUE\n");
		return;
	}
	if(s>m){
		for(i=1;i<=m;i++){
			putchar(c[i]);
		}
	}
	else{
		for(i=m+2;i<=n;i++){
			putchar(c[i]);
		}
	}
}
int main(){
	into();
	work();
	return 0;
}

 

posted on   TLECODE  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示