[Baltic2014]friends
首先想想暴力的做法,枚举加入的字符,然后判断删去这个字符后两个长度为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的更多相关文章
- BZOJ 3916: [Baltic2014]friends( hash )
字符串哈希..然后枚举每一位+各种判断就行了 ----------------------------------------------------------------------------- ...
- BZOJ_3916_[Baltic2014]friends_hash
BZOJ_3916_[Baltic2014]friends_hash 题意: 有三个好朋友喜欢在一起玩游戏,A君写下一个字符串S,B君将其复制一遍得到T,C君在T的任意位置(包括首尾)插入一个字符得到 ...
- 【BZOJ】3916: [Baltic2014]friends
http://www.lydsy.com/JudgeOnline/problem.php?id=3916 #include <bits/stdc++.h> using namespace ...
- BZOJ3919 : [Baltic2014]portals
预处理出每个点上下左右能延伸到的最远点以及到它们的距离的最小值md. 然后spfa,一个点除了可以以1的代价到达四周的点之外,还可以以md+1的代价到达四个方向能到达的最远点. #include< ...
- bzoj3917: [Baltic2014]sequence
Description 序列A由从N开始的连续K个数按顺序构成,现在将A中的每个数只保留某一个数码,记为序列B,给定K和B,求可能的最小的N Input 第一行一个数K,第二行K个数B_i Outp ...
- BZOJ3916: [Baltic2014]friends
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 复习一下hash(然后被傻叉错误卡了半天TAT... 取出一个字串:h[r]-h[l-1 ...
- 【题解】 bzoj3916: [Baltic2014]friends (字符串Hash)
题面戳我 Solution 首先长度为偶数可以直接判掉 然后我们可以枚举删的位置,通过预处理的\(hash\),判断剩余部分是否划分成两个一样的 判重要注意,我们把字符串分为三个部分\(L_l+1+L ...
- BZOJ3916: [Baltic2014]friends
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 题解:随便hash.刚开始看错题WA了N发.(我连双hash都写了!) 代码: #inc ...
- bzoj3918 [Baltic2014]Postman
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3918 [题解] 每日至少更一题啊qwq凑任务(迷 明显猜个结论:随便搜环就行了 然后搜环姿势 ...
随机推荐
- 比较ArrayList和LinkedList的异同
1.ArrayList是实现了基于动态数组的数据结构,而LinkedList是基于链表的数据结构: 2.对于随机访问get和set,ArrayList要优于LinkedList; 3.对于添加和删除操 ...
- Sql Server 中使用日期遍历
一个存储过程小案例,内容如下: declare @dt datetime set @dt='2016-01-01' while (@dt<='2016-12-31') begin -- 转换字符 ...
- mysql 中 max_allowed_packet 查询和修改
mysql 会根据配置文件限制 server 接收的数据包的大小. 有时候大的插入和更新会被 max_allowed_packet 参数限制,报如下错误: Packet > ). You can ...
- 为 HTTP/2 头压缩专门设计的 HPACK
HTTP/2 对消息头采用 HPACK 进行压缩传输,能够节省消息头占用的网络的流量.如何理解 HPACK 压缩呢? 如果我们约定将常用的请求头的参数用一些特殊的编号来表示,比如 GET ...
- 列表 enumerat 解包, 针对索引和元素
dic = [1,2,3,4,5] for a,b in enumerate(dic): print(a,b) # a就是索引 b是元素
- 使用AJAX实现分页
Fenye.html <!DOCTYPE html> <html> <head> <title>分页</title> </head&g ...
- 转:xdebug在linux下的安装教程
原文:xdebug在linux下的安装教程 [注意,本人是PHP7.1.7 Nginx ,第7步没有做,但是xdebug.so就已经在PHP的扩展文件夹里面了.目录是phpinfo的extension ...
- for、for..in、forEach、$.each等循环性能测试
var num = 10000000,arr = []; for(i=0;i<num;i++){ arr[i] = i+2; } //1) 使用 for 循环 function test1() ...
- FFmpeg精确时间截取视频
简介: 之前用到过FFmpeg截取过音频和视频发现,截取的视频文件时间不是很准确,今天便系统的学习了一下FFmpeg截取视频的知识 参考: https://zhuanlan.zhihu.com/p/2 ...
- SystemLogParser ArcGIS Server运维工程师的好助手
单位使用ArcGIS Server发布了众多GIS服务,作为系统运维工程师,一直希望能有一种便捷的方式能获得服务的访问情况,Esri推出的SystemLogParser这款小程序就是不错的辅助工具,而 ...