UVA - 10324 Zeros and Ones

Description

Given a string of 0‘s and
1‘s up to
1000000 characters long and indices
i and
j, you are to answer a question whether all characters between position
min(i,j) and position
max(i,j) (inclusive) are the same.

Input

There are multiple cases on input. The first line of each case gives a string of 0‘s and 1‘s. The next line contains a positive integer n giving the number of queries for this case. The next n lines
contain queries, one per line. Each query is given by two non-negative integers, i and j. For each query, you are to print Yes if all characters in the string between position min(i,j) and
position max(i,j) are the same, and No otherwise.

Output

Each case on output should start with a heading as in the sample below. The input ends with an empty string that is a line containing only the new line character, this string should not be processed. The input
may also with end of file. So keep check for both.

Sample Input

0000011111
3
0 5
4 2
5 9
01010101010101010101010101111111111111111111111111111111111110000000000000000
5
4 4
25 60
1 3
62 76
24 62
1
1
0 0

Sample Output

Case 1:
No
Yes
Yes
Case 2:
Yes
Yes
No
Yes
No
Case 3:
Yes

题意:给你一个01串,推断连续的字串[l, r]是否仅仅含同一个种字符

思路:用dp[i]记录到当前位置有多少个连续的同样字符,假设询问的区间的大小<=dp[r],那么代表就是能够的

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1000005;

int dp[maxn];
int n, q;
char str[maxn];

int main() {
	int cas = 1;
	while (scanf("%s", str) != EOF) {
		int len = strlen(str);
		dp[0] = 1;
		for (int i = 1; i < len; i++)
			if (str[i] == str[i-1])
				dp[i] = dp[i-1] + 1;
			else dp[i] = 1;
		printf("Case %d:\n", cas++);
		int l, r;
		scanf("%d", &q);
		while (q--) {
			scanf("%d%d", &l, &r);
			if (l > r)
				swap(l, r);
			if (r - l + 1 <= dp[r])
				printf("Yes\n");
			else printf("No\n");
		}
	}
	return 0;
}
时间: 2024-08-10 02:10:56

UVA - 10324 Zeros and Ones的相关文章

UVA 12063 Zeros and Ones (数位dp)

Binary numbers and their pattern of bits are always very interesting to computer programmers. In this problem you need to count the number of positive binary numbers that have the following properties: The numbers are exactly N bits wide and they hav

UVA 12063 Zeros and Ones(三维dp)

题意:给你n.k,问你有多少个n为二进制的数(无前导零)的0与1一样多,且是k的倍数 题解:对于每个k都计算一次dp,dp[i][j][kk][l]表示i位有j个1模k等于kk且第一位为l(0/1) 再次预处理mod[i][j]表示1的i次方模j等于几,具体看代码注释 import java.util.Scanner; public class Main{ static int Maxn=65; static int Maxk=101; //前i个数有j个1模给定的值余k且第一位为1或者0的总个

UVA 12063(dp记忆化)

UVA - 12063 Zeros and Ones Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Binary numbers and their pattern of bits are always very interesting to computer programmers. In this problem you need to c

一些DP杂题

1. [HNOI2001] 产品加工 一道简单的背包,然而我还是写了很久QAQ 时间范围是都小于5 显然考虑一维背包,dp[i]表示目前A消耗了i的最小B消耗 注意 if(b[i]) dp[j]=dp[j]+b[i]; else dp[j]=1e9+7; 可以用B则直接转移,否则要把上一次的这个状态设为正无穷,只能用后两个转移. //Twenty #include<cstdio> #include<cstdlib> #include<iostream> #include

UVa 12063 (DP) Zeros and Ones

题意: 找出长度为n.0和1个数相等.没有前导0且为k的倍数的二进制数的个数. 分析: 这道题要用动态规划来做. 设dp(zeros, ones, mod)为有zeros个0,ones个1,除以k的余数为mod的二进制数的个数,则状态转移方程为: dp(zeros + 1, ones, (mod>>1) % k) += dp(zeros, ones, mod) dp(zeros, ones + 1, ((mod>>1)+1) % k) += dp(zeros, ones, mod)

UVA 10047 - The Monocycle

题目如下:  Problem A: The Monocycle  A monocycle is a cycle that runs on one wheel and the one we will be considering is a bit more special. It has a solid wheel colored with five differentcolors as shown in the figure: The colored segments make equal an

UVa 12505 Searching in sqrt(n)

传送门 一开始在vjudge上看到这题时,标的来源是CSU 1120,第八届湖南省赛D题“平方根大搜索”.今天交题时CSU突然跪了,后来查了一下看哪家OJ还挂了这道题,竟然发现这题是出自UVA的,而且用的原题. ------------------------------------------------------------------------------------------------------------------ time limit 5s In binary, the

UVa - 12664 - Interesting Calculator

先上题目: 12664 Interesting CalculatorThere is an interesting calculator. It has 3 rows of button.• Row 1: button 0, 1, 2, 3, . . . , 9. Pressing each button appends that digit to the end of the display.• Row 2: button +0, +1, +2, +3, . . . , +9. Pressin

UVA 1252 十五 Twenty Questions

十五 Twenty Questions Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 1252 Appoint description:  System Crawler  (2015-08-25) Description Consider a closed world and a set of features that are defined f