A - Scary Martian Word

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

There are different superstitions on different planets. Martians laugh at Earthlings suffering Triskaidekaphobia and Hexakosioihexekontahexaphobia. And Earthlings make jokes on the fact that there is a scary word in Martian language. Martians are not only afraid of the word itself, but also of all the words that are obtained by rearrangement of letters in this word.
The Martian alphabet has 729 000 letters. Correspondent Ovchinnikov, who lives on Mars and studies Martian language, represents Martian letters as triplets of symbols with ASCII codes from 33 to 122. He has recently written a book about life and culture on Mars. Before sending the book into press Ovchinnikov wants to count the number of substrings in the text of the book which are frightening for Martians.

Input

The first line contains the scary Martian word, consisting of at most 8 000 Martian letters. The second line contains the text of Ovchinnikov's book, which is at most 500 000 Martian letters long. Both the scary word and the text of the book contain at least one letter. Every Martian letter is represented by the triplet of symbols with ASCII codes from 33 to 122, each letter separated from the next one by a whitespace.

Output

Output the number of substrings in the book by correspondent Ovchinnikov, which are frightening for Martians.

Sample Input

input output
aaa bbb ccc
aaa aaa bbb ccc aaa zzz aaa bbb ccc
3

Notes

Two substrings “aaa bbb ccc” (starting from the second and the seventh positions in the text) and a substring “bbb ccc aaa” are scary for the Martians.
 



#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int cur[maxn*maxn*maxn];
int hash[maxn*maxn*maxn];
char s[];
int change(char *s){
int res=;
for(int i=;i<;i++){
res=maxn*res+s[i];
}
return res;
}
int h[];
int main(){
char c;
int len=;
while(scanf("%s",s)!=EOF){
int temp=change(s);
hash[temp]++;
len++;
c=getchar(); if(c=='\n')
break;
}
int ans=;
int cnt=;
for(int i=;;i++){
scanf("%s",s);
int d=change(s);
h[i]=d;
if(cur[d]<hash[d])
cnt++;
cur[d]++;
if(i>=len){
d=h[i-len];
if(cur[d]<=hash[d])
cnt--;
cur[d]--;
}
if(cnt==len)
ans++;
c=getchar();
if(c=='\n')
break;
}
printf("%d\n",ans);
return ;
}

哈希URAL 1941 - Scary Martian Word的更多相关文章

  1. 1941. Scary Martian Word

    1941. Scary Martian Word 这道题 一个长度为3的字符串视为 一个 火星文 字母(ASCII 33-122) ,给出一个火星人认为恐怖的单词(由火星字母组成) 然后 给你一篇文章 ...

  2. URAL 1941

    比赛的时候三个点没有优化成功.其实也没有想到哈希成数.然后就变成了只要一个长度和scary相等的区间内所有数字个数都是相等的.那么就是符合题意的.于是.为了不TLE我们不能对txt每个位置遍历 的同时 ...

  3. 学习Word2vec

    有感于最近接触到的一些关于深度学习的知识,遂打算找个东西来加深理解.首选的就是以前有过接触,且火爆程度非同一般的word2vec.严格来说,word2vec的三层模型还不能算是完整意义上的深度学习,本 ...

  4. 软工结对项目之词频统计update

    队友 胡展瑞 031602215 作业页面 GitHub 具体分工 111500206 赵畅:负责WordCount的升级,添加新的命令行参数支持(自定义输入输出文件,权重词频统计,词组统计等所有新功 ...

  5. Java对象创建的过程及对象的内存布局与访问定位

    这里以HotSpot为例,且所说的对象指普通的Java对象,不包括数组和Class对象等. 1.对象创建的过程 1.类加载.解析.初始化:虚拟机遇到new时先检查此指令的参数是否能在常量池中找到类的符 ...

  6. UVALive 6320

    .............先让我哭一会....... 因为样例三.... 没敢敲...然后确实是样例错了...然后...上次比赛刚做过的一道类似的题...URAL 1941...大水题啊......悔 ...

  7. synchronized运行原理以及优化

    线程安全问题 线程不安全: 当多线程并发访问临界资源时(可共享的对象),如果破坏原子操作,可能会造成数据不一致. 临界资源:共享资源(同一对象),一次仅允许一个线程使用,才可以保证其正确性. 原子操作 ...

  8. LintCode 550 · Top K Frequent Words II

    题目描述 题目链接 思路 由于要统计每个字符串的次数,以及字典序,所以,我们需要把用户每次add的字符串封装成一个对象,这个对象中包括了这个字符串和这个字符串出现的次数. 假设我们封装的对象如下: p ...

  9. 使用加强堆结构解决topK问题

    作者:Grey 原文地址: 使用加强堆结构解决topK问题 题目描述 LintCode 550 · Top K Frequent Words II 思路 由于要统计每个字符串的次数,以及字典序,所以, ...

随机推荐

  1. 基于建模的视觉定位(SFM-Based Positioning)

    具体方法来自我参与的这篇journal: Vision-Based Positioning for Internet-of-Vehicles, IEEE Transactions on Intelli ...

  2. Visual Studio Code快捷键_Linux

    Keyboard shortcuts for Linux Basic editing Ctrl + X Cut line(empty selection) Ctrk + C   Copy line(e ...

  3. Linux下 tar 命令详解

    tar 是 unix/linux下的打包器 [解压] 输入命令: # tar  -zxvf  filename.tar.gz 参数解释: z :表示 tar 包是被 gzip 压缩过的 (后缀是.ta ...

  4. composer安装教程(Linux版)

    composer 是款不错的工具,那么如何进行安装composer呢 如果您是linux系统或是mac系统 请先确定是否安装了curl linux安装curl   1 yum install -y c ...

  5. php面向对象(2)值传递

    PHP中值传递方式,2中 值传递:传递的时候,拷贝的是数据本身.默认都是值传递 结果:传递完成,有了2份同样的数据,且2个变量“相互独立”,不会相互影响 引用传递:传递的时候,拷贝的是引用关系(数据的 ...

  6. 利用python进行坐标提取以及筛选(文件操作的小应用)

    由于目前暂时还未学习到python关于数据处理的模块方面的知识,且刚好最近朋友发来一份坐标数据文件(txt格式),让我帮他对其进行筛选, 因此利用了最近刚学过的python文件处理操作以及以前所学的基 ...

  7. 基于appium的app自动化测试框架

    基于appium框架的app自动化测试 App自动化测试主要难点在于环境的搭建,appium完全是基于selenium进行的扩展,所以app测试框架也是基于web测试框架开发的 一.设备连接 (即构建 ...

  8. linux-shell——02

    Linux命令的通用命令格式 :命令字 [选项] [参数] 选项: 作用:用于调节命令的具体功能 "-"引导短格式选项(单个字符) EX:“-l” "--"引导 ...

  9. [Bzoj1034][ZJOI2008]泡泡堂BNB(贪心)

    Description 题目链接 Solution 这题就是一个贪心, 如果最弱的能赢对方最弱的就赢 否则最强的能赢对面最强的就赢 否则最弱的换对面最强 Code #include <cstdi ...

  10. 17-比赛1 D - IPC Trainers (贪心 + 优先队列)

    题目描述 本次印度编程训练营(Indian Programming Camp,IPC)共请到了 N 名教练.训练营的日程安排有 M 天,每天最多上一节课.第 i 名教练在第 Di 天到达,直到训练营结 ...