[COCI2017-2018#3] Programiranje
題目
Description
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.
Little Leticija正在準備程式設計考試。雖然她已經解決了很多工,但還有一個任務尚未解決,所以她正在向你尋求幫助。您將獲得單詞S和Q查詢。在每個查詢中,給出正整數A,B,C和D.假設單詞X由單詞S中位置A和B之間的字母組成,而單詞S中位置C和D之間的字母組成單詞Y.如果可以以某種方式重新排列單詞Y中的字母並獲得單詞X,則必須回答。
Input
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.
** 輸入格式:
** 第一行輸入包含單詞S(1≤| S |≤50000)。| S | 表示單詞S中的字元數,由英文字母的小寫字母組成。第二行輸入包含正整數Q(1≤Q≤50000)。
以下Q行中的每一行包含來自任務的四個整數A,B,C i D(1≤A≤B≤| S |且1≤C≤D≤| S |)。
Output
For each query, output “DA” (Croatian for yes) if it is possible, and “NE” (Croatian for no) if it is not.
** 輸出格式:
** 對於每個查詢,如果可能,輸出“DA”(克羅埃西亞語為yes),如果不可能,則輸出“NE”(克羅埃西亞語為否)。
Sample Input
vodevovode 2 5 8 3 6 2 5 3 6
Sample Output
NE DA
思路
一道很水的題;
我們可以設 $f[i][j]$ 表示第 $1-i$ 個位置 第 $j$ 個字母出現的次數;
然後列舉判斷一下就好了;
程式碼
#include<bits/stdc++.h> #define re register typedef long long ll; typedef unsigned long long ull; using namespace std; inline ll read() { ll a=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') {a=a*10+c-'0'; c=getchar();} return a*f; } char s[50010]; ll n,m; ll f[50010][30]; int main() { scanf("%s",s+1);//讀入 n=strlen(s+1); for(re ll i=1;i<=n;i++) { ll x=s[i]-'a'; for(re ll j=0;j<26;j++) f[i][j]=f[i-1][j]; f[i][x]++;//記錄 1-i ,中每個字母出現的次數 } m=read(); for(re ll i=1;i<=m;i++) { ll a=read(),b=read(),c=read(),d=read(); ll flag=1; for(re ll i=0;i<26;i++) { if(f[b][i]-f[a-1][i]!=f[d][i]-f[c-1][i])//判斷不多解釋 { flag=0; break; } } if(flag) puts("DA"); else puts("NE"); } }