7-3 Counting Lower-case Letters (25 分)
Given a string consisting of lower-case letters chosen from a-z, you can obtain numbers of occurrence of each letter.
Now you are given a string and several intervals of it. And for each interval, you are also given a lower-case letter. Your task is to count the number of corresponding letter in each interval.
Input Specification:
Each input file contains one test case. In each test case, the first line contains a string consisting of no more than 50,000 characters. And the second line contains an integer K (<=10
5
, the number of queries). Then the following K lines each give information in the format
B E c
where B and E denote the beginning and the end of interval, respectively, and c is a character (a-z). Hence the characters in the given string are indexed from 0 to N−1, where N is the length of the string.
Output Specification:
For each query, print in a line the count of corresponding letter.
Sample Input:
aaabcadeefagg
3
0 3 a
2 5 b
4 7 d
Sample Output:
3
1
1
#include<bits/stdc++.h>
using namespace std;
int main(){
//freopen("1.txt","r",stdin);
vector<char>s;
vector<int> in;
char a;
int i,n,j=0;
int count=0;
int st,ed;
char b;
while((a=getchar())!='\n'){
s.push_back(a);
}
cin>>n;
for(i=0;i<n;i++){
cin>>st>>ed>>b;
for(j=st;j<=ed;j++){
if(s[j]==b){
count++;
}
}
in.push_back(count);
count=0;
}
for(int m;m<n;m++){
cout<<in[m]<<endl;
}
return 0;
}
很简单的一道题,但是莫名时间超时…有想法的朋友可以一起来讨论下。