嘟嘟嘟

首先想想暴力的做法,枚举加入的字符,然后判断删去这个字符后两个长度为n / 2的字符串是否相等,复杂度O(n2)。

所以可以想办法把判断复杂度降低到O(1),那自然就想到hash了。hash是能做到O(n)预处理,然后O(1)比较的。

取一段的hash值:hash[L, R] = hash[1, R] - hash[1, L - 1] * baseR - L + 1.这个也好理解,就是前面的hash[1, L - 1]为整个hash值贡献了hash[1, L - 1] * baseR - L + 1,减去即可。

思路就这么明显~~只不过合并hash值得时候得想清楚了……

 #include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a) memset(a, 0, sizeof(a))
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
const int INF = 0x3f3f3f3f;
const ull base = ;
const int eps = 1e-;
const int maxn = 2e6 + ;
inline ll read()
{
ll ans = ;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) {last = ch; ch = getchar();}
while(isdigit(ch)) {ans = ans * + ch - ''; ch = getchar();}
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < ) x = -x, putchar('-');
if(x >= ) write(x / );
putchar(x % + '');
} int n, mid;
char s[maxn];
ull has[maxn], f[maxn], las = ;
int del, tot = ; ull get(int L, int R)
{
return has[R] - has[L - ] * f[R - L + ];
}
int judge(int x)
{
ull h1, h2;
if(x < mid)
{
h1 = get(, x - ) * f[mid - x] + get(x + , mid);
h2 = get(mid + , n);
}
else if(x > mid)
{
h1 = get(, mid - );
h2 = get(mid, x - ) * f[n - x] + get(x + , n);
}
else
{
h1 = get(, x - );
h2 = get(x + , n);
}
if(h1 == h2)
{
del = x;
if(h1 == las) return ;
las = h1; return ;
}
return ;
} int main()
{
n = read(); scanf("%s", s + );
if(!(n & )) {puts("NOT POSSIBLE"); return ;}
f[] = ;
for(int i = ; i <= n; ++i) f[i] = f[i - ] * base;
for(int i = ; i <= n; ++i) has[i] = has[i - ] * base + s[i] - 'A' + ;
mid = (n >> ) + ;
for(int i = ; i <= n; ++i) tot += judge(i);
if(!tot) puts("NOT POSSIBLE");
else if(tot > ) puts("NOT UNIQUE");
else
{
for(int i = , cnt = ; cnt <= (n >> ); ++i, cnt++)
{
if(i == del) cnt--;
else putchar(s[i]);
}
enter;
}
return ;
}

[Baltic2014]friends的更多相关文章

  1. BZOJ 3916: [Baltic2014]friends( hash )

    字符串哈希..然后枚举每一位+各种判断就行了 ----------------------------------------------------------------------------- ...

  2. BZOJ_3916_[Baltic2014]friends_hash

    BZOJ_3916_[Baltic2014]friends_hash 题意: 有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到 ...

  3. 【BZOJ】3916: [Baltic2014]friends

    http://www.lydsy.com/JudgeOnline/problem.php?id=3916 #include <bits/stdc++.h> using namespace ...

  4. BZOJ3919 : [Baltic2014]portals

    预处理出每个点上下左右能延伸到的最远点以及到它们的距离的最小值md. 然后spfa,一个点除了可以以1的代价到达四周的点之外,还可以以md+1的代价到达四个方向能到达的最远点. #include< ...

  5. bzoj3917: [Baltic2014]sequence

    Description  序列A由从N开始的连续K个数按顺序构成,现在将A中的每个数只保留某一个数码,记为序列B,给定K和B,求可能的最小的N Input 第一行一个数K,第二行K个数B_i Outp ...

  6. BZOJ3916: [Baltic2014]friends

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 复习一下hash(然后被傻叉错误卡了半天TAT... 取出一个字串:h[r]-h[l-1 ...

  7. 【题解】 bzoj3916: [Baltic2014]friends (字符串Hash)

    题面戳我 Solution 首先长度为偶数可以直接判掉 然后我们可以枚举删的位置,通过预处理的\(hash\),判断剩余部分是否划分成两个一样的 判重要注意,我们把字符串分为三个部分\(L_l+1+L ...

  8. BZOJ3916: [Baltic2014]friends

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 题解:随便hash.刚开始看错题WA了N发.(我连双hash都写了!) 代码: #inc ...

  9. bzoj3918 [Baltic2014]Postman

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3918 [题解] 每日至少更一题啊qwq凑任务(迷 明显猜个结论:随便搜环就行了 然后搜环姿势 ...

随机推荐

  1. CALayer及其子类

    前言:这个系列要更新Core Animation的内容,但是CALayer是Core Animation的基础. 一 CALayer是什么? 摘自官网的一句话-Layers Provide the B ...

  2. 用正则表达式修改html字符串的所有div的style样式

    最近项目中有一接口返回的一个字段是html格式的字符串,里边每个div中style的高度是固定的px,然后再手机端显示的时候发现,div中的内容重叠了:效果图如下: 对应的div如下: 由于高度固定, ...

  3. ECMAScript 5和ECMAScript6的新特性以及浏览器支持情况

    ECMAScript简介: 它是一种由Ecma国际(前身为欧洲计算机制造商协会)制定和发布的脚本语言规范,javascript在它基础上经行了自己的封装.但通常来说,术语ECMAScript和java ...

  4. Codeforces 750 F:New Year and Finding Roots

    传送门 首先如果一开始就找到了一个叶子,那么暴力去递归找它的父亲,每次随机一个方向(除了已知的儿子)走深度次,如果走到了一个叶子就不是这个方向 (设根的深度为 \(1\))这样子最后到达深度为 \(3 ...

  5. opencv3.2.0形态学滤波之形态学梯度、顶帽、黑帽

    /*一.形态学梯度 (1)含义:是膨胀图和腐蚀图之差 (2)数学表达式:dst=morph-grad(src,element) =dilate(src,element) - erode(src,ele ...

  6. CentOS7系列--3.1CentOS7中配置NFS服务

    CentOS7配置NFS服务 1. 配置NFS服务器端 1.1. 安装nfs-utils软件 [root@server1 ~]# yum install -y nfs-utils Loaded plu ...

  7. RESULT_OK,RESULT_CANCELED,RESULT_FIRST_USER

    RESULT_OK是执行结果,有RESULT_OK,RESULT_CANCELED,RESULT_FIRST_USER 在调用系统app时返回时RESULT_CANCELED如字面意思代表取消,RES ...

  8. Android 原生 MediaPlayer 和 MediaCodec 的区别和联系(二)

    目录: (3)Android 官方网站 对 MediaPlayer的介绍 正文:  Android 官方网站 对 MediaPlayer的介绍         MediaPlayer      pub ...

  9. CSS 小结笔记之清除浮动

    浮动是一个非常好用的属性,但是有时会出现一些问题,需要进行清除浮动.例如 <!DOCTYPE html> <html lang="en"> <head ...

  10. eval解析json字符串

    场景:在springMVC,手动拼接的list,转成本json字符串后,传到前台, 解决:需要解析成json对象,获取对象的属性,动态生成table. 首先,以下是后台准备好的list,list中有两 ...