[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 ...
随机推荐
- js深入研究之函数内的函数
第一种 function foo() { ; function bar() { a *= ; } bar(); return a; } 第二种 function foo() { ; function ...
- Windows下MySQL双向同步及环形同步的实现
记录一下这次做的双向同步及环形同步吧,都是最简单的实现: 具体实现之前,先说些与之有关的内容吧,大部分内容都是网上的,操作步骤则是亲自测试之后记录下的: 一. 数据同步的几种方式: 1. 触发器,在数 ...
- UIView添加支持代码块的手势
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(a ...
- 【Eclipse DDMS】 Can't bind to local 8600 for debugger
问题原因: 电脑上同时安装了Eclipse 和Android Studio两个ide. 关键是使用eclipse adb连接真机时候,android studio也处于运行状态,后者默认也是要连接ad ...
- Spring的工作原理核心组件和应用
Spring框架 Spring 是管理多个java类的容器框架,注意是类不管理接口. Spring 的主要功能 Ioc 反转控制和 DI 依赖注入. 注入的方式可以是构造函数赋值也可以是 set方法赋 ...
- Struts2小结
Struts 2是在WebWork2基础发展而来的. 注意:struts 2和struts 1在代码风格上几乎不一样. Struts 2 相比Struts 1的优点: 1.在软件设计上Struts 2 ...
- 探讨socket引发SIGPIPE信号的问题
我写socket相关的程序也不是一天两天了,在我的记忆中,只要处理好recv(或read)的返回值中<0,==0,>0三种情况,程序便不会有什么问题.但最近在看公司的源代码时,发现代码中直 ...
- Beyond Compare 忽略两个文件内容的顺序比较文件内容(xjl456852原创)
有时两个文件内容的顺序是不固定的,对比时需要忽略文件顺序进行对比. 可以这样设置: 点击菜单下面工具栏按钮: 点击Format旁的三角,选择Sorted,就会按文件的顺序排序比较.忽略了文件内容顺序的 ...
- JMeter录制脚本
Jmeter 是一个非常流行的性能测试工具,虽然与LoadRunner相比有很多不足,比如:它结果分析能力没有LoadRunner详细:很它的优点也有很多: l 开源,他是一款开源的免费软件,使用它你 ...
- 让你不再纠结GitHub:Git起步
一.关于版本控制 版本控制是一种记录若干文件内容变化,以便将来查阅特定版本修订情况的系统.我们通常仅对保存着软件源代码的文本文件做版本控制,但实际上,你可以对任何类型的文件进行版本控制. 采用版本控制 ...