嘟嘟嘟

首先想想暴力的做法,枚举加入的字符,然后判断删去这个字符后两个长度为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. Storm框架:如何消费RabbitMq消息(代码案例)

    1.定义拓扑topology public class MessageTopology { public static void main(String[] args) throws Exceptio ...

  2. windows多线程窗口程序设计

    掌握windows基于消息驱动的窗口应用程序设计的基本方法,掌握窗口程序资源的概念与设计,掌握常用的消息的程序处理方法,掌握文字图形输出相关函数编程.掌握设计的基本方法(选项),掌握时钟消息设计动画程 ...

  3. 深入理解MyBatis的原理(三):配置文件(上)

    前言:前文提到一个入门的demo,从这里开始,会了解深入 MyBatis 的配置,本文讲解 MyBatis 的配置文件的用法. 目录 1.properties 元素 2.设置(settings) 3. ...

  4. Q:链表的倒数第K个元素

    问题:如何得到链表中的倒数第k个元素?   一种简单的思路是遍历链表一遍,并统计出链表中节点的数目,然后计算出倒数第k个元素到链表头节点的元素的距离,然后得到对应的结果.但是,我们能否有一种更加简便的 ...

  5. MVC 导出Execl 的总结几种方式 (三)

    第三种方式呢,就是借用第三方插件 NPOI 来实现Execl 导出 第一步:在NuGut包中下载NPOI 组件 第二步:编辑控制器代码 public FileResult ExcelNewKeyPro ...

  6. Hibernate的多对多实例

    在完成了一对多的实例的基础上,继续做多对多实例.例子是老师和学生,一个老师教多个学生,一个学生也有多个老师. 文档结构如图:

  7. 关于Dubbo异常之Data length too large

    最近几日发现生产环境项目打出的日志,每天都在30~50G以上,寻找多次发现问题: 首先查看日志只看到大批量的json数据输出,这是方法查询后的返回值输出,期初以为是自己打了logger,结果寻找多次, ...

  8. java获取本月第一天和最后一天

    public class CalendarTest { public static void main(String[] args) { // 获取当前年份.月份.日期 Calendar cale = ...

  9. 报表在vista和win7下无法浏览应用的解决办法

     对于vista和win7系统,报表工具有着良好的兼容性,无论是设计器还是实际应用.有些客户在安装报表设计报表的时候没有遇到问题,但是在这两种系统下会发现无法启动应用,或者打开设计器自带的ie浏览 ...

  10. oracle 实现主键自增

    -- 创建表 drop table test; create table test(id number(10), name varchar2(10)); -- 创建对列 drop sequence s ...