[COCI2017-2018#3] Programiranje(前缀和)

[COCI2017-2018#3] Programiranje

题面翻译

Little Leticija 正在准备编程考试。虽然她已经解决了很多任务,但还有一个任务尚未解决,于是她向你寻求帮助。

有一个单词 S S S Q Q Q 次询问。在每次询问中,给出正整数 A A A B B B C C C D D D。假设单词 X X X 由单词 S S S 中位置 A A A B B B 及其之间的字母组成,而单词 Y Y Y 由位置 C C C D D D 及其之间的字母组成。您需要回答是否能以某种方式重新排列单词 Y Y Y 中的字母得到单词 X X X

【输入格式】

第一行输入包含单词 S S S 1 ≤ ∣ S ∣ ≤ 50000 1\le\lvert S\rvert\le50000 1S50000)。 ∣ S ∣ \lvert S\rvert S 表示单词 S S S 中的字符数。 S S S 完全由英文小写字母组成。

第二行输入包含正整数 Q Q Q 1 ≤ Q ≤ 50000 1\le Q\le50000 1Q50000)。
以下 Q Q Q 行中的每一行包含四个整数 A A A B B B C C C D D D 1 ≤ A ≤ B ≤ ∣ S ∣ 1\le A\le B\le\lvert S\rvert 1ABS 1 ≤ C ≤ D ≤ ∣ S ∣ 1\le C\le D\le\lvert S\rvert 1CDS)。

【输出格式】

对于每次询问,如果可能,输出DA(即克罗地亚语的“是”),如果不可能,则输出NE(克语的“否”)。

【说明/提示】

对于 50 % 50\% 50% 的测试点,有 1 ≤ ∣ S ∣ ≤ 1000 1\le\lvert S\rvert\le1000 1S1000 1 ≤ Q ≤ 1000 1\le Q\le1000 1Q1000

对于 100 % 100\% 100% 的测试点,有 1 ≤ ∣ S ∣ ≤ 50000 1\le\lvert S\rvert\le50000 1S50000 1 ≤ Q ≤ 50000 1\le Q\le50000 1Q50000 1 ≤ A ≤ B ≤ ∣ S ∣ 1\le A\le B\le\lvert S\rvert 1ABS 1 ≤ C ≤ D ≤ ∣ S ∣ 1\le C\le D\le\lvert S\rvert 1CDS

样例 #3 的解释:在第一次询问中, X = v o v o X=\tt vovo X=vovo Y = d e v o Y=\tt devo Y=devo。在第二次询问中, X = o d e v X=\tt odev X=odev Y = d e v o Y=\tt devo Y=devo

题目描述

Little Leticija is preparing for a programming exam. Even though she has solved a lot of tasks, there’s one still left unsolved, so she is asking you for help. You are given the word S and Q queries. In each query, you are given positive integers A, B, C and D. Let’s say that word X consists of letters between positions A and B in word S, and word Y from letters between positions C and D in word S. For each query, you must answer if it is possible to somehow rearrange the letters in word Y and obtain word X.

输入格式

The first line of input contains the word S (1 ≤ |S| ≤ 50 000). |S| denotes the number of characters in word S, which consists of lowercase letters of the English alphabet. The second line of input contains the positive integer Q (1 ≤ Q ≤ 50 000).

Each of the following Q lines contains four integers A, B, C i D (1 ≤ A ≤ B ≤ |S| and 1 ≤ C ≤ D ≤ |S| ) from the task.

输出格式

For each query, output “DA” (Croatian for yes) if it is possible, and “NE” (Croatian for no) if it is not.

样例 #1

样例输入 #1

kileanimal
2
2 2 7 7
1 4 6 7

样例输出 #1

DA
NE

样例 #2

样例输入 #2

abababba
2
3 5 1 3
1 2 7 8

样例输出 #2

DA
DA

样例 #3

样例输入 #3

vodevovode
2
5 8 3 6
2 5 3 6

样例输出 #3

NE
DA

提示

In test cases worth 50% of total points, it will hold: 1 ≤ |S| ≤ 1000 and 1 ≤ Q ≤ 1000.

Clarification​ ​of​ ​the​ ​third​ ​test​ ​case:

In the first query, X=”vovo”, and Y=”devo”. In the second query, X=”odev”, and Y=”devo”.

代码

#include<iostream>
#include<cstring>

using namespace std;

const int N = 50000+10;

int q,sum[N][30];
char s[N];

int main(){
	cin>>s+1;
	int len=strlen(s+1);
	for(int i=1;i<=len;i++){
		for(int j=0;j<26;j++)sum[i][j]=sum[i-1][j];
		sum[i][s[i]-'a']++;
	}
	
	cin>>q;
	
	int i;
	
	while(q--){
	    int a,b,c,d;
		cin>>a>>b>>c>>d;
		for(i=0;i<26;i++)
			if(sum[b][i]-sum[a-1][i]!=sum[d][i]-sum[c-1][i]){
				puts("NE");
				i=27;
			}
		if(i==26)puts("DA");
	}
	
	return 0;
}
  • 25
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值