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. ...
随机推荐
- 【转】linux之cp/scp命令+scp命令详解
linux之cp/scp命令+scp命令详解 名称:cp 使用权限:所有使用者 使用方式: cp [options] source dest cp [options] source... dire ...
- Python之GUI的最终选择(Tkinter)
首先,Tkinter是Python默认的GUI库,想IDLE就是用Tkinter设计出来的,因此直接导入Tkinter模块就可以啦 1 import tkinter (1)Tkinter初体验: 1 ...
- 区间内x的出现个数(主席树)
题目大概:求区间内x出现的次数 出题人yjy Description ZJK 给你一个长度为 n 的数列和 m 次询问,每次询问从第 l 个到第 r 个数中,数 x 出现了多少次.Input第一行一个 ...
- rvm 安装ruby环境报错curl: (35) error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure
很可能是rvm仓库版本过低,运行以下命令: rvm get head
- Mysql ACID与隔离级别
参考: https://blog.csdn.net/csdnxingyuntian/article/details/57081233 https://www.cnblogs.com/huanongyi ...
- spring注解注入properties配置文件
早期,如果需要通过spring读取properties文件中的配置信息,都需要在XML文件中配置文件读取方式. 基于XML的读取方式: <bean class="org.springf ...
- ACM-ICPC 2018 南京赛区网络预赛 L. Magical Girl Haze 最短路+分层图
类似题解 There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u, ...
- CenterOS下从零起步简单部署RockMongo
使用Mongodb,对于调试Query,查看Collection等状态,有Rockmongo是非常方便的. 研究了下Rockmongo的部署,主要是依赖PHP环境的web服务器,当前有两种服务器,一种 ...
- springmvc基础知识及注解
SpringMVC 1.概念 Spring的MVC框架是一个基于DispatcherServlet的MVC框架,主要由DispatcherServlet.处理器映射.处理器.视图解析器.视图组成.每一 ...
- UVa 821 网页跳跃(Floyd)
https://vjudge.net/problem/UVA-821 题意:给出一个有向图,任意两点都可相互到达,求任意两点的最短距离的平均值. 思路:求两点的最短距离,用Floyd算法很方便,最后加 ...