B. Ilya and Queries
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

Input

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).

Output

Print m integers — the answers to the queries in the order in which they are given in the input.

Sample test(s)
input
......
4
3 4
2 3
1 6
2 6
output
1
1
5
4
input
#..###
5
1 3
5 6
1 5
3 6
3 4
output
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的更多相关文章

  1. 递推 Codeforces Round #186 (Div. 2) B. Ilya and Queries

    题目传送门 /* 递推:用cnt记录前缀值,查询区间时,两个区间相减 */ #include <cstdio> #include <algorithm> #include &l ...

  2. [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 ...

  3. 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  ...

  4. 贪心 Codeforces Round #297 (Div. 2) C. Ilya and Sticks

    题目传送门 /* 题意:给n个棍子,组成的矩形面积和最大,每根棍子可以-1 贪心:排序后,相邻的进行比较,若可以读入x[p++],然后两两相乘相加就可以了 */ #include <cstdio ...

  5. Codeforces Round #186 (Div. 2)

    A. Ilya and Bank Account 模拟. B. Ilya and Queries 前缀和. C. Ilya and Matrix 考虑每个元素的贡献. 边长为\(2^n\)时,贡献为最 ...

  6. 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/ ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #186 (Div. 2).D

    纠结的一道dp. 状态转移方程还是比较好想的,优化比较纠结 D. Ilya and Roads time limit per test 3 seconds memory limit per test ...

随机推荐

  1. objc runtime 动态增加属性

    objective-c中,有类别可以在不修改源码的基础上增加方法:近排在看别人的开源代码时,发现还可以动态增加属性.而且是在运行时,太牛B了. 使用运行时库,必须要先引入 objc/runtime.h ...

  2. 学习ASP.NET的一些学习资源

    ASP.NET学习相关资源 当我们在决定选择哪一个编程语言来做web开发的时候,很难选择,php.java.python这些语言是开源的,有很多的学习资源,但是当我们决定学习ASP.NET的时候,微软 ...

  3. 同行blog收集

    姜楠:http://www.slyar.com/blog/ 赵劼:http://blog.zhaojie.me/ 研究者July:http://my.csdn.net/v_JULY_v 王卓群:htt ...

  4. cf413E Maze 2D

    E. Maze 2D time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  5. Hadoop 1、在虚拟机上进行 HDFS 安装

    一.准备条件 1.四台Linux虚拟机(1台NameNode节点,1台Secondary节点(Secondary和其中1台DataNode共用),外加2台DataNode) 2.下载Hadoop版本, ...

  6. Java IO 概述

    输入和输出-数据源和目标媒介 术语“输入”和“输出”有时候会有一点让人疑惑.一个应用程序的输入往往是另一个应用程序的输出.那么OutputStream流到底是一个输出到目的地的流呢,还是一个产生输出的 ...

  7. Windows下模拟Linux开发

    1.背景 Linux环境下开发是大势所趋,也是开发者必须掌握的技能.然windows系统已深入人心,实在不想放弃windows下的成熟应用,因此可以在Windows上模拟一个Linux系统.这样就满足 ...

  8. How to face setbacks

    I’ve been in a bad mood since I started on the American Accent. I became even more upset when I adde ...

  9. 同一台电脑启动两个或多个tomcat

    今天要在机子的tomcat上部署新的项目,需要访问的端口为80,与之前不同. 但要求不能更改原tomcat部署项目的端口,因为该tomcat内的项目正在对外使用中,且不能断开服务器. 那么,我就需要再 ...

  10. LoadRunner监控windows资源报错Monitor name :Windows Resources. Cannot connect to machine

    目标机:被监控的机器,windows server 2008 R2. 测试机:执行control的机器,windows7 操作:在测试机上执行Control,添加windows的监控 问题现象:Mon ...