Computer Science, asked by candy108, 1 year ago

Similar Char
- Problem Description
Tahir and Mamta are woking in a project in TCS. Tahir being a problem solver came up with an interesting probler
Problem consists of a string of length N and contains only small case alphabets.
It will be followed by Q queries, in which each query will contain an integer P (1<=P<=N) denoting a position within
Mamta's task is to find the alphabet present at that location and determine the number of occurrence of same alpl
Mamta is busy with her office work. Therefore, she asked you to help her.
Constraints
1 <= N <= 500000
S consisting of small case alphabets
WWWWWWW
1 <= Q <= 10000
1 <= p <= N
WWW
- Input Format
WWW
First line contains an integer N, denoting the length of string.
Second line contains string Sitself consists of small case alphabets only ('a' - 'z).
C
SEE
omo
ya
WEWS
Tamir
Third line contains an integer Q denoting number of queries that will be asked.
Next o lines contains an integer P (1 <= P <= N) for which you need to find the number occurrence of character presen​

Answers

Answered by silaskanth2
5

Answer:

#include <iostream>  

#include <string>

using namespace std;

void count1(string s, int c[], int n)  

{  

   int res, a;

for (int i = 0; i < n ; i++)

   {    

       res = 0;  

 a = c[i];

       char temp = s[a-1];

       

       for(int j = 0; j < a-1; j++)

       {

           if (s[j] == temp)  

               res++;

       }

       

       cout<<res<<endl;

   }

}  

 

int main()  

{  

   unsigned int len;

   unsigned short int quer;

   string str;

   cin>>len;

   cin>>str;

   cin>>quer;

   int querry[quer];

   for (int i = 0; i < quer; i++)

   {

       cin>>querry[i];

   }

   count1(str, querry, quer);  

   return 0;  

}

Explanation:

Please do not copy as I am also attending Codevita exam. Understand the logic by looking at this program. If found copied, we both will be disqualified.

Similar questions