题解:hash

至今不会unsigned long long 的输出

把B扔进map

找A[mid+1][lenA]在A[1][mid]中的位置

把A[1][mid]贴两遍(套路)

枚举A[mid+1][lenA]在A[1][mid]中出现的位置,把其他位置的hash值求出来,在map里查有多少符合的B串

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<map>
  5. #include<algorithm>
  6. using namespace std;
  7. const int maxn=8000009;
  8. typedef unsigned long long uLint;
  9.  
  10. int n,m,lenA,lenB,mid;
  11. char s[maxn];
  12. long long ans;
  13.  
  14. int r;
  15. uLint h[maxn];
  16. uLint fac[maxn];
  17. map<uLint,int>ma;
  18. uLint a[maxn];
  19. void Addstring(){
  20. int cnt=0;
  21. uLint midstring=0;
  22. for(int i=mid+1;i<=lenA;++i)midstring=midstring*233+s[i];
  23.  
  24. for(int i=1;i<=mid;++i)s[i+mid]=s[i];
  25.  
  26. h[0]=0;
  27. for(int i=1;i<=mid+mid;++i)h[i]=h[i-1]*233+s[i];
  28. for(int i=1;i<=mid;++i){
  29. uLint tm=h[i+r-1]-h[i-1]*fac[r];
  30. if(tm!=midstring)continue;
  31. a[++cnt]=h[i+mid-1]-h[i+r-1]*fac[mid-r];
  32. }
  33. sort(a+1,a+1+cnt);
  34. cnt=unique(a+1,a+1+cnt)-a-1;
  35. for(int i=1;i<=cnt;++i)ma[a[i]]++;
  36. }
  37.  
  38. int main(){
  39. scanf("%d%d%d%d",&n,&m,&lenA,&lenB);
  40. fac[0]=1;
  41. for(int i=1;i<=lenA+lenA;++i)fac[i]=fac[i-1]*233;
  42. mid=(lenA+lenB)>>1;
  43. r=lenA-mid;
  44.  
  45. while(n--){
  46. scanf("%s",s+1);
  47. Addstring();
  48. }
  49. while(m--){
  50. scanf("%s",s+1);
  51. uLint tm=0;
  52. for(int i=1;i<=lenB;++i)tm=tm*233+s[i];
  53. ans=ans+ma[tm];
  54. }
  55. cout<<ans<<endl;
  56. return 0;
  57. }

  

BZOJ 4084 [Sdoi2015]双旋转字符串的更多相关文章

  1. [bzoj4084][Sdoi2015]双旋转字符串_hash

    双旋转字符串 bzoj-4084 Sdoi-2015 题目大意:给定两个字符串集合 S 和 T .其中 S 中的所有字符串长度都恰好为 N ,而 T 中所有字符串长度都恰好为 M .且 N+M 恰好为 ...

  2. bzoj 4084 双旋转字符串

    给两个集合A,B,找满足要求的(a,b)的对数,可以计算对于a,哪些b成立. 还有就是字符串hash的使用,感觉平时用字符串hash太少了. /**************************** ...

  3. 【bzoj4084】【sdoi2015】双旋转字符串

    题解 首先题中说了$n>=m$; 分成的循环串左右两边为本质相同的单循环串循环串,分别长为$l = \frac{n + m}{2} $; 所以$S$串的前$l$位为双循环串的一半$S1$,后一半 ...

  4. 剑指Offer面试题:34.翻转单词顺序VS左旋转字符串

    一.题目一:翻转单词顺序 1.1 题目说明 题目一:输入一个英文句子,翻转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标点符号和普通字母一样处理.例如输入字符串"I am a st ...

  5. 【面试题042】翻转单词顺序VS左旋转字符串

    [面试题042]翻转单词顺序VS左旋转字符串 题目一:     输入一个英文句子,反转句子中单词的顺序,但单词内字符的顺序不变.为简单起见,标点符号和普通字母一样处理.     例如输入字符串“I a ...

  6. lintcode :旋转字符串

    题目: 旋转字符串 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 样例 对于字符串 "abcdefg". offset=0 => "abcdef ...

  7. 九度OJ 1362 左旋转字符串(Move!Move!!Move!!!)【算法】

    题目地址:http://ac.jobdu.com/problem.php?pid=1362 题目描述: 汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运 ...

  8. LintCode 面试题 旋转字符串

    1.题目描述 题目链接:http://www.lintcode.com/zh-cn/problem/rotate-string/ 给定一个字符串和一个偏移量,根据偏移量旋转字符串(从左向右旋转) 2. ...

  9. 1289 大鱼吃小鱼 1305 Pairwise Sum and Divide 1344 走格子 1347 旋转字符串 1381 硬币游戏

    1289 大鱼吃小鱼 有N条鱼每条鱼的位置及大小均不同,他们沿着X轴游动,有的向左,有的向右.游动的速度是一样的,两条鱼相遇大鱼会吃掉小鱼.从左到右给出每条鱼的大小和游动的方向(0表示向左,1表示向右 ...

随机推荐

  1. spring-页面模板配置

    一个可用的freemaker配置: ... @Bean public ViewResolver viewResolver() { FreeMarkerViewResolver fr = new Fre ...

  2. STM32之GD25Q256应用笔记

    前言 使用标准SPI指令格式: 3字节地址模式或4字节地址模式: 支持SPI 总线 模式0 和 模式3: 存储容量32MB: 时钟频率范围:80Mhz – 104Mhz: 主机:STM32F103ZE ...

  3. G. Repeat it

    G. Repeat it time limit per test 2.0 s memory limit per test 64 MB input standard input output stand ...

  4. VC 插入excel

    CString ePath,iPath; m_ePath.GetWindowText(ePath); m_iPath.GetWindowText(iPath); _Application app; W ...

  5. 【pwnable.kr】blackjack

    又一道pwnable nc pwnable.kr 9009 读题找到源代码在:http://cboard.cprogramming.com/c-programming/114023-simple-bl ...

  6. 使用git提交远程仓库

    git pull    更新 git add 文件名   将文件添加到暂存区 git commit -m ‘注释’   提交 git push origin master   提交到远程仓库

  7. SpringMVC原理及流程解析

    前言 春节期间宅在家里闲来无事,对SpringMVC进行了比较深入的了解,将之前模糊不清的地方基本摸索清楚了,特此撰文总结记录一下. 正文 一.一个请求为什么会调用到SpringMVC框架里? 首先问 ...

  8. 三十、SAP中的内置图标

    一.选择[编辑]->[模式] 二.选中写入模式 三.选中图标 四.选择我们需要的图标 五.选中图标后,选择复制 六.点击之后,会回到代码界面,如下 七.执行结果如下,显示出了一个神奇的图标

  9. 115-PHP实例化和不实例化输出类常量

    <?php class boy{ //定义男孩类 const sex='男'; public $age=15; } echo '不实例化对象输出类常量:'.boy::sex; $boy=new ...

  10. 计算方法执行完的耗时 c#

    Stopwatch watch = Stopwatch.StartNew(); //要执行的方法 test(); watch.Stop(); Console.WriteLine(string.Form ...