[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 ...
随机推荐
- DOCKER功能练习
都是书上的示例,慢慢进入..
- Codeforces 429B Working out
http://codeforces.com/contest/429/problem/B 题意:一个从左下到右上,一个从左上到右下,要求只相交一次,求整个路径和的最大值 思路:发现可以枚举交点,然后算到 ...
- 2015必须要看的APP源码
多媒体类型 哔哩哔哩(bilibili)客户端源码 一个高仿哔哩哔哩(bilibili)客户端的开源项目,效果不错 下载地址: http://www.apkbus.com/forum.php?mod= ...
- [置顶] 【原创分享】嵌入式linux应用之内核移植定制篇-前篇(linux-3.8.12 mini2440)--20130824
移植的话其实很早就做过了,不过那时用的友善定制的老版本2.6.32 驱动什么的全部弄好了,仅仅用默认配置而已.基本不用改动什么,很简单. 内核更新其实非常的快,今天我就用个3.8.12来移植. 当然, ...
- javascript 典型闭包的用法
<body><input type="radio" id="radio1" name="readionGroup" /&g ...
- phpcms:一、安装及新建模板
1.复制D:\WWW\phpcms\phpcms\templates\目录下的default文件粘贴在当前目录下,并重命名为新模板名字(youpinzhiyuan2012) 2.打开D:\WWW\ph ...
- HTML--控制小人自由移动
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- python3-day4(装饰器)
一.基本 第一波 #### def foo(): print 'foo' foo #表示是函数 foo() #表示执行foo函数 #### 第二波 #### def foo ...
- POJ 3450 Corporate Identity (KMP+暴搞)
题意: 给定N个字符串,寻找最长的公共字串,如果长度相同,则输出字典序最小的那个. 找其中一个字符串,枚举它的所有的字串,然后,逐个kmp比较.......相当暴力,可二分优化. #include & ...
- Android开发之DatePickerDialog与TimePickerDialog的功能和使用方法具体解释
DatePickerDialog与TimePickerDialog的功能比較简单,使用方法也非常easy.仅仅要以下两步就可以. Ø 通过newkeyword创建DatePickerDialog.T ...