题目链接:传送门

参考文章:传送门

思路:

(1)对字符串进行翻译,求出s1(未翻译),s2(已翻译)字符串,并分别对s1,s2进行hash操作。

(2)比较s1的后x位和s2的前x位的hash值,求出最长的长度的中间位置。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn = ;
char s1[maxn],s2[maxn],ss[maxn];
int fy[maxn];
typedef unsigned long long ULL;
ULL hash1[maxn],hash2[maxn],pre[maxn],base=;
ULL getl(int l,int r,ULL arr[])
{
return arr[r]-arr[l-]*pre[r-l+];
}
void Init()
{
pre[]=;
for(int i=;i<maxn;i++) pre[i]=pre[i-]*base;
}
int main(void)
{
int T,i;
scanf("%d",&T);
Init();
while(T--)
{
scanf("%s%s",ss,s1+);
int l=strlen(ss),len=strlen(s1+);
for(i=;i<l;i++) fy[ss[i]-'a']=i;
for(i=;i<=len;i++)
{
s2[i]=fy[s1[i]-'a']+'a';
}
for(hash1[]=,hash2[]=,i=;i<=len;i++)
{
hash1[i]=hash1[i-]*base+(ULL)s1[i];
hash2[i]=hash2[i-]*base+(ULL)s2[i];
}
//cout<<s2+1<<endl;
int ans=len;
for(i=len;i<len*;i++) //最长长度一定是偶数,
{
if(i&) continue; //判断偶数
int t1=i/;
int t2=len-t1;
ULL a1=getl(,t2,hash2);
ULL a2=getl(len-t2+,len,hash1);
if(a1==a2)
{
ans=t1;break;
}
}
for(i=;i<=ans;i++) printf("%c",s1[i]);
for(i=;i<=ans;i++) printf("%c",s2[i]);
printf("\n");
}
return ;
}

hdu-4300(字符串hash)的更多相关文章

  1. hdu 4821 字符串hash+map判重 String (长春市赛区I题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...

  2. HDU 4821 字符串hash

    题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...

  3. hdu 1880 字符串hash

    /*普通的hsah 由于元素太多 空间很小..hash碰撞很厉害.30分*/ #include<iostream> #include<cstdio> #include<c ...

  4. HDU 1880 字符串hash 入门题

    Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...

  5. Hdu 1800 字符串hash

    题目链接 题意: 给出n(n<=3000)个字符串(长度<30,数字组成,肯能存在前导0), 问该序列最少可以分成多少个单调序列.可以转化成求相同字符串的个数的最大值 附上代码: /*** ...

  6. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

  7. HDU 5763 Another Meaning dp+字符串hash || DP+KMP

    题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思 例如:str:hehehe   sub:hehe 那么,有**he.he** ...

  8. HDU 1880 魔咒词典 (字符串hash)

    <题目链接> 题目大意: 就是每个字符串有一个配套的对应字符串,询问的时候,无论输出其中的哪一个字符串,输出另一个,如果不存在这个字符串,直接输出"what?". 解题 ...

  9. 算法笔记--字符串hash

    概述: 主要用于字符串的匹配. 定义hash函数: H(c)=(c1bm-1 +c2bm-2 +...+cmb0)mod h 对于字符串c中l-r区间的hash值: H(l,r)=H(1,r)-H(1 ...

  10. HDU 4622 Reincarnation Hash解法详解

    今天想学字符串hash是怎么弄的.就看到了这题模板题 http://acm.hdu.edu.cn/showproblem.php?pid=4622 刚开始当然不懂啦,然后就上网搜解法.很多都是什么后缀 ...

随机推荐

  1. Leading and Trailing (数论)

    Leading and Trailing https://vjudge.net/contest/288520#problem/E You are given two integers: n and k ...

  2. 从零开始写bootloader(2)

    下图是设置内核启动参数的存放图示,由于bootloader启动内核时,需要给内核传输一些启动参数,但是由于当bootloader把内核 启动之后,程序就跳转到内核中执行了,再也不会回到bootload ...

  3. 第一个springMVC小程序

    1首先配置一个前端控制器,在web.xml文件中配置(dispatcherservlet) <!-- 前端控制器配置 --> <servlet> <servlet-nam ...

  4. JS判断变量类型

    typeof v 只能用于识别基础类型,不能识别对象 v instanceof MyClass 判断类型 Object.prototype.toString.call(v.p) === "[ ...

  5. TZOJ 4712 Double Shortest Paths(最小费用最大流)

    描述 Alice and Bob are walking in an ancient maze with a lot of caves and one-way passages connecting ...

  6. Javascript(es2016) import和require用法和区别

    http://blog.csdn.net/chinaycheng/article/details/52559439 其中这个总结很到位: 加载方式 规范 命令 特点 运行时加载 CommonJS/AM ...

  7. 662. Maximum Width of Binary Tree二叉树的最大宽度

    [抄题]: Given a binary tree, write a function to get the maximum width of the given tree. The width of ...

  8. 纯css实现div中未知尺寸图片的垂直居中

    1.淘宝的方法 在曾经的"淘宝UED招聘"中有这样一道题目: “使用纯CSS实现未知尺寸的图片(但高宽都小于200px)在200px的正方形容器中水平和垂直居中.” 当然出题并不是 ...

  9. 微信小程序模板中使用循环

    原创文章 本篇介绍如何在模板里使用循环. 方法一: template.wxml,该文件内容为: <template name="msgItem1"> <block ...

  10. LIS LCS 最长上升子序列 最长公共子序列 ...

    最长上升子序列,问题定义:http://blog.csdn.net/chenwenshi/article/details/6027086 代码: public static void getData( ...