1001 字符串“水”题(二进制,map,哈希)
1001: 字符串“水”题
时间限制: 1 Sec 内存限制: 128 MB
提交: 210 解决: 39
[提交][状态][讨论版]
题目描述
输入
接下来 T 行,每行有一个只包含小写字母的字符串。
输出
样例输入
3
a
aabbcc
abcabc
样例输出
0
6
1
提示
这道题挺不错的,
用二进制的低0-25位分别保存'a'-'z'出现的次数,然后根据相同状态统计,
见代码,
#include <bits/stdc++.h>
using namespace std;
map<int,int>mmap;
char str[];
int main ()
{
int T;
scanf("%d",&T);
while(T--) {
mmap.clear();
scanf("%s",str);
int len = strlen(str);
int state=;
long long sum=;
for(int i=; i<len; i++) {
state^=(<<(str[i]-'a'));
if(state==) {
sum++;
}
sum+=mmap[state];
mmap[state]++;//相同状态出现次数
}
printf("%lld\n",sum);
}
return ;
}
后来可能数据加强了,上面代码超时了。
优化一下,先哈希然后存map,
#include <bits/stdc++.h>
#define maxn 34567 using namespace std;
typedef long long LL; char str[];
map<int,int>sk[maxn];
void solve()
{
for(int i=;i<maxn;i++)
sk[i].clear();
int len=strlen(str);
int ret=;
LL ans=;
sk[][]=;
for(int i=; i<len; i++)
{
int now=str[i]-'a';
ret^=(<<now);
ans+=sk[ret%maxn][ret];
sk[ret%maxn][ret]++;
}
printf("%lld\n",ans);
} int main()
{
int T;
scanf("%d%*c",&T);
while(T--)
{
scanf("%s",str);
solve();
}
return ;
}
1001 字符串“水”题(二进制,map,哈希)的更多相关文章
- 1222: FJ的字符串 [水题]
1222: FJ的字符串 [水题] 时间限制: 1 Sec 内存限制: 128 MB 提交: 92 解决: 20 统计 题目描述 FJ在沙盘上写了这样一些字符串: A1 = “A” A2 = ...
- 第十一届“蓝狐网络杯”湖南省大学生计算机程序设计竞赛 B - 大还是小? 字符串水题
B - 大还是小? Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Description 输入两个实数,判断第一个数大 ...
- Codeforces Round #309 (Div. 2) A. Kyoya and Photobooks 字符串水题
A. Kyoya and Photobooks Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- HDU ACM 1073 Online Judge ->字符串水题
分析:水题. #include<iostream> using namespace std; #define N 5050 char a[N],b[N],tmp[N]; void Read ...
- HDU4891_The Great Pan_字符串水题
2014多校第五题,当时题面上的10^5写成105,我们大家都wa了几发,改正后我和一血就差几秒…不能忍 题目:http://acm.hdu.edu.cn/showproblem.php?pid=48 ...
- Codeforces Round #309 (Div. 2) B. Ohana Cleans Up 字符串水题
B. Ohana Cleans Up Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/554/pr ...
- uva 10252 - Common Permutation 字符串水题
题意:給定兩個小寫的字串a與b,請印出皆出現在兩字串中的字母,出現的字母由a~z的順序印出,若同字母出現不只一次,請重複印出但不能超過任一字串中出現的次數.(from Ruby兔) 很水,直接比较输出 ...
- Codeforces 1105B:Zuhair and Strings(字符串水题)
time limit per test: 1 second memory limit per test: 256 megabytes input: standard input output: sta ...
- 牛客 109B 好位置 (字符串水题)
大意: 给定字符串$s1,s2$, 对于$s1$中所有与$s2$相等的子序列$t$, $t$在$s1$中的下标定义为好位置. 求$s1$是否所有位置都是好位置. 显然$s1$的前缀要与$s2$相等, ...
随机推荐
- 如何在有input() 语句下断点调试(内附高清无码福利)
困扰了半天,一直没找到如何在含有输入语句的情况下用pycharm进行断点调试(调试的同时进行输入交互), But 经过尝试,还是找到了~~~ 通过debug可以快速的找到报错信息,以及观察程序每步的运 ...
- windows server2003/2008中权限账户
在windows server 2003与windows server 2008 R2中,查看文件夹权限时,尤其是用cacls命令查看时,经常会见nt authority system这样的用户信息. ...
- C#数组学习
1.多维数组 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespa ...
- Multi-thread & Multi-process
关于多进程和多线程,教科书上最经典的一句话是“进程是资源分配的最小单位,线程是CPU调度的最小单位”. 对于到底是使用多进程还是多线程, 要根据实际情况来判断,选择更适合的. 具体情况,可以参考下面: ...
- CSS中input输入框点击时去掉外边框方法【outline:medium;】----CSS学习
CSS 中添加 outline:medium; JS 控制焦点: $("#CUSTOM_PHONE").focus(function(event){ // this.attr(&q ...
- five application :Labeling features
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- yii异常处理架构
使用方法: use \yii\base\ErrorException; try { exec("curl http://xxx",$out,$retno); if(0 !== $r ...
- this对象解析
this在js中有着非常广泛的应用,但其所指的对象也常常让人摸不着头脑,简而言之: this指的就是调用函数的对象,最常见的莫过以下几种 1.直接使用函数,则为window对象 function a( ...
- myisam表修复
数据库myisam引擎表损坏修复步骤: 1.进入到表目录文件下 # myisamchk -of comments.MYI 2. # myisamchk -r comments.MYI 3. # ...
- 关于Pytorch的二维tensor的gather和scatter_操作用法分析
看得不明不白(我在下一篇中写了如何理解gather的用法) gather是一个比较复杂的操作,对一个2维tensor,输出的每个元素如下: out[i][j] = input[index[i][j]] ...