[Codeforces Round #186 (Div. 2)] B. Ilya and Queries
2 seconds
256 megabytes
standard input
standard output
Ilya the Lion wants to help all his friends with passing exams. They need to solve the following problem to pass the IT exam.
You've got string s = s1s2... sn (n is the length of the string), consisting only of characters "." and "#" and m queries. Each query is described by a pair of integers li, ri (1 ≤ li < ri ≤ n). The answer to the query li, ri is the number of such integers i (li ≤ i < ri), that si = si + 1.
Ilya the Lion wants to help his friends but is there anyone to help him? Help Ilya, solve the problem.
The first line contains string s of length n (2 ≤ n ≤ 105). It is guaranteed that the given string only consists of characters "." and "#".
The next line contains integer m (1 ≤ m ≤ 105) — the number of queries. Each of the next m lines contains the description of the corresponding query. The i-th line contains integers li, ri (1 ≤ li < ri ≤ n).
Print m integers — the answers to the queries in the order in which they are given in the input.
......
4
3 4
2 3
1 6
2 6
1
1
5
4
#..###
5
1 3
5 6
1 5
3 6
3 4
1
1
2
2
0 题解:一个字符串只有.或者#字符,给你一段区间[x,y],y>x,在这个区间里面统计s[i]=s[i+1]最多个数。
线性动态规划:转移方程
if(a[i]==a[i-1])
f[i]=f[i-1]+1;
else f[i]=f[i-1];
题目地址:http://codeforces.com/contest/313/problem/B
代码:
#include<stdio.h>
#include<string.h> int i,j,n,x,y,m,
f[];
char a[]; int
main()
{
gets(a);
m=strlen(a);
f[]=;
for(i=;i<m;i++)
{
if(a[i]==a[i-])
f[i]=f[i-]+;
else f[i]=f[i-];
} scanf("%d",&n);
while(n--)
{
scanf("%d%d",&x,&y);
printf("%d\n",f[y-]-f[x-]);
} return ;
}
[Codeforces Round #186 (Div. 2)] B. Ilya and Queries的更多相关文章
- 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries
题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...
- [Codeforces Round #186 (Div. 2)] A. Ilya and Bank Account
A. Ilya and Bank Account time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks
题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...
- Codeforces Round #186 (Div. 2)
A. Ilya and Bank Account 模拟. B. Ilya and Queries 前缀和. C. Ilya and Matrix 考虑每个元素的贡献. 边长为\(2^n\)时,贡献为最 ...
- Codeforces Round #311 (Div. 2) A. Ilya and Diplomas 水题
A. Ilya and Diplomas Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/ ...
- Codeforces Round #430 (Div. 2) C. Ilya And The Tree
地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 second ...
- Codeforces Round #293 (Div. 2) D. Ilya and Escalator 概率DP
D. Ilya and Escalator time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #186 (Div. 2).D
纠结的一道dp. 状态转移方程还是比较好想的,优化比较纠结 D. Ilya and Roads time limit per test 3 seconds memory limit per test ...
随机推荐
- cf Ping-Pong (Easy Version)
http://codeforces.com/contest/320/problem/B 这道题看了很长时间没看懂, 就是个dfs: #include <cstdio> #include & ...
- DirectUI中模态对话框和菜单的原理(自己控制整个Windows消息循环。或者,用菜单模拟窗体打开时用SetCapture取得控制权,一旦窗体收到WM_CAPTURECHANGED消息就把窗体退出)
经常有人问关于模态对话框和系统菜单内部实现原理方面的问题, 因为系统通过API隐藏了太多细节,这2个问题确实令初学者甚至是有经验的开发者困扰, 下面是我个人的一些经验总结. 先说模态对话框,外部看模态 ...
- inux xsel 拷贝复制命令行输出放在系统剪贴板上
转载自:http://oldratlee.com/post/2012-12-23/command-output-to-clip 为什么要这么做?直接把命令的输出(比如 grep/awk/sed/fin ...
- Eclipse无法打开“Failed to load the JNI shared library”
解决方案一 这是因为JDK配置错误所导致的现象. 一般说来,新购笔记本会预装64位的windows系统,而在网上下载软件时,32位会优先出现在页面中(现在来说是这个情况,但我认为未来64位会越来越普及 ...
- C/C++捕获段错误,打印出错的具体位置(精确到哪一行)
修订:2013-02-16 其实还可以使用 glibc 的 backtrace_symbols 函数,把栈帧各返回地址里面的数字地址翻译成符号描述的 修订:2011-06-11 背景知识: · 在li ...
- bzoj 1192
http://www.lydsy.com/JudgeOnline/problem.php?id=1192 好像学过一个东西: [0..2^(N+1)-1]内的数都的都可以由2^0,2^1,...,2^ ...
- 黑马程序员_Java面向对象1_封装
3.面向对象_封装 3.1面向对象概念 3.1.1理解面向对象 面向对象是相对面向过程而言 面向对象和面向过程都是一种思想 面向过程:强调的是功能行为(执行者) 面向对象:将功能封装进对象,强调具备了 ...
- About Adultism and why things ar the way they are
About - Adultism About Adultism and why things ar the way they are In this page we will try to clari ...
- JSON解析---初识
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式 全然独立于语言的文本格式 易于人阅读和编写 易于解析和生成 (网络传输速度快) JSON语法规则 数据在 ...
- 利用iptables实现基于端口的网络流量统计
如何统计某个应用的网络流量(包括网络流入量和网络流出量)问题,可以转换成如何基于端口号进行网络流量统计的问题.大部分网络应用程序都是传输层及以上的协议,因此基于端口号(tcp, udp)统计网络流量基 ...