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;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!