Monday, December 2, 2013

c++ solution to codechef "Holes in the text" problem

problem link: http://www.codechef.com/problems/HOLES/
i suggest you to look at: http://discuss.codechef.com/questions/4184/holes-editorial

Holes in the text

All submissions for this problem are available.

Chef wrote some text on a piece of paper and now he wants to know how many holes are in the text. What is a hole? If you think of the paper as the plane and a letter as a curve on the plane, then each letter divides the plane into regions. For example letters "A", "D", "O", "P", "R" divide the plane into two regions so we say these letters each have one hole. Similarly, letter "B" has two holes and letters such as "C", "E", "F", "K" have no holes. We say that the number of holes in the text is equal to the total number of holes in the letters of the text. Help Chef to determine how many holes are in the text.

Input

The first line contains a single integer T <= 40, the number of test cases. T test cases follow. The only line of each test case contains a non-empty text composed only of uppercase letters of English alphabet. The length of the text is less then 100. There are no any spaces in the input.

Output

For each test case, output a single line containing the number of holes in the corresponding text.

Example

Input:
2
CODECHEF
DRINKEATCODE

Output:
2
5

my c++ solution to codechef "Holes in the text" problem: http://ideone.com/Jj4OLR

#include <iostream>
#include <cstring>
using namespace std;

int main() {
 // your code goes here
 int a;
 char str[100];
 cin>>a;
 while (a--) {
  cin>>str;
  int x=0;
  for(int i=0; i<strlen(str); i++) {
   if (str[i]=='A') x=x+1;
   else if (str[i]=='D') x=x+1;
   else if (str[i]=='O') x=x+1;
   else if (str[i]=='P') x=x+1;
   else if (str[i]=='Q') x=x+1;
   else if (str[i]=='R') x=x+1;
   else if (str[i]=='B') x=x+2;
   else x=x+0;
  }
  cout<<x<<endl;
 }
 return 0;
}

No comments:

Post a Comment