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. 手动安装Apache+PHP+MYSQL及环境配置

    先准备好软件: Apache官方下载地址:apache_2.0.55-win32-x86-no_ssl.msi,更多版本在这里: php官方下载地址:php-5.0.5-Win32.zip,更多镜像下 ...

  2. maven中import scope依赖方式解决单继承问题的理解

    在maven多模块项目中,为了保持模块间依赖的统一,常规做法是在parent model中,使用dependencyManagement预定义所有模块需要用到的dependency(依赖) <d ...

  3. React后端管理系统-商品详情detail组件

    import React from 'react'; import MUtil from 'util/mm.jsx' import Product from 'service/product-serv ...

  4. pdo->prepare 返回false的问题总结

    报错信息: Fatal error: Call to a member function execute() on a non-object 一般是pdo->prepare 返回了false导致 ...

  5. 提高篇(1):RMQ问题与ST表

    RMQ是英文Range Minimum/Maximum Query的缩写,是询问某个区间内的最值,这里讲一种解法:ST算法 ST算法通常用在要多次(10^6级别)询问区间最值的问题中,相比于线段树,它 ...

  6. docker基础——关于安装、常用指令以及镜像制作初体验

    为什么使用docker docker就是一个轻量级的虚拟机,他解决的是服务迁移部署的时候环境配置问题.比如常见的web服务依赖于jdk.Tomcat.数据库等工具,迁移项目就需要在新的机器重新配置这些 ...

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

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

  8. JRE和JDK区别

    JRE: Java Runtime Environment JDK:Java Development Kit JRE顾名思义是java运行时环境, 包含了java虚拟机,java基础类库. 是使用ja ...

  9. B1008 数组元素循环右移问题 (20分)

    B1008 数组元素循环右移问题 (20分) 思路 1 2 3 4 5 6 5 6 1 2 3 4 6个数,循环右移2位. 也可以理解为 先翻转 6 5 4 3 2 1 然后再两部分,分别翻转 5 6 ...

  10. 输入cin对象的用法

    #include<iostream> using namespace std; int main() { int carrots ; cout << "How man ...