URAL 1941
比赛的时候三个点没有优化成功。其实也没有想到哈希成数。然后就变成了只要一个长度和scary相等的区间内所有数字个数都是相等的。那么就是符合题意的。于是。为了不TLE我们不能对txt每个位置遍历 的同时还对scary每个位置遍历。
这个代码好有智慧。数据不极端的情况下是不会超时的。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std; #define maxn 2000010
#define maxm 32010 char txt[maxn], sc[maxm]; // 输入的字符串
int th[], sh[]; // th[]存txt中每个字符的值是多少.sh[]数组存scary字符串中每个字符对应值有多少个。 int main()
{
while(gets(sc))
{
int cnt1 = , cnt2 = ;
memset(sh, , sizeof(sh));
memset(th, , sizeof(th)); int len = strlen(sc);
for (int i=; i<len; i+=)
{
int temp = sc[i] - ;
sh[temp]++;
cnt1++;
} gets(txt);
len = strlen(txt);
for (int i=; i<len; i+=)
{
int temp = txt[i] - ;
th[cnt2] = temp;
cnt2++;
} int st = ; // 从第一个点依次遍历从每个点开始到此后cnt1区间是不是对应值个数相等。
int ans = ;
for (int i=; i<cnt2; ++i)
{
sh[th[i]]--;
while (sh[th[i]]<) //比较过程中遇见一个个数不足 或者 scary里没出现的字符起点就要从这里开始
{
sh[th[st]]++; //同时起点前面那些都回溯没有访问过
st++;
}
if (i-st+ == cnt1) //如果此时长度和scary相同。一定就是个数相同.ans++;
{
ans++;
sh[th[st]]++;
st++;
}
}
printf("%d\n", ans);
}
return ;
}
L哦哦K
URAL 1941的更多相关文章
- 哈希URAL 1941 - Scary Martian Word
A - Scary Martian Word Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- UVALive 6320
.............先让我哭一会....... 因为样例三.... 没敢敲...然后确实是样例错了...然后...上次比赛刚做过的一道类似的题...URAL 1941...大水题啊......悔 ...
- 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome
题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...
- ural 2071. Juice Cocktails
2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...
- ural 2073. Log Files
2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...
- ural 2070. Interesting Numbers
2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...
- ural 2069. Hard Rock
2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...
- ural 2068. Game of Nuts
2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...
- ural 2067. Friends and Berries
2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...
随机推荐
- 微信小程序——3、逻辑js文件
逻辑层js文件 微信小程序前端进行了层次划分,分为逻辑层和视图层.逻辑层实现对数据的加工和处理.与HTML页面相似,逻辑层使用JavaScript编写.逻辑层将数据处理后发送至视图层,同时接受视图层的 ...
- VC 线程池
参照:http://www.cnblogs.com/kzloser/archive/2013/03/11/2909221.html 参照:http://blog.csdn.net/pjchen/art ...
- python创建MySQL多实例-1
python创建MySQL多实例-1 前言 什么是多实例 多实例就是允许在同一台机器上创建另外一套不同配置文件的数据库,他们之间是相互独立的,主要有以下特点, 1> 不能同时使用一个端口 2&g ...
- POJ 2373 Dividing the Path (单调队列优化DP)题解
思路: 设dp[i]为覆盖i所用的最小数量,那么dp[i] = min(dp[k] + 1),其中i - 2b <= k <= i -2a,所以可以手动开一个单调递增的队列,队首元素就是k ...
- linux 安装MySql 5.7.20
1.下载文件(https://pan.baidu.com/s/1c1VBcHy)放到目录:/usr/local/ 2.解压 cd /usr/local/ tar -zxvf mysql-5.7.20- ...
- Java LinkedList源码剖析
LinkedList 本文github地址 总体介绍 LinkedList同时实现了List接口和Deque接口,也就是说它既可以看作一个顺序容器,又可以看作一个队列(Queue),同时又可以看作一个 ...
- jquery.cookie.js中$.cookie() 使用方法
定义:让网站服务器把少量数据储存到客户端的硬盘或内存,从客户端的硬盘读取数据的一种技术: 下载与引入:jquery.cookie.js基于jquery:先引入jquery,再引入:jquery.coo ...
- Android程序示例
目录 Android代码示例 OptionsMenu ImageButton CheckBox & RadioButton Context Menu快捷菜单 Key Event ListVie ...
- shell 无限循环输出时间
#!/bin/bash while(true) do date >> /home/k/a.log sleep done 查看 tail -f /home/k/a.log
- [ios]object-c math.h里的数学计算公式介绍
参考:http://blog.csdn.net/yuhuangc/article/details/7639117 头文件:<math.h> 1. 三角函数 double sin (dou ...