Friday, January 10, 2014

codechef TIDRICE - "popular rice recipe" solution and guidance

codechef TIDRICE - "popular rice recipe": http://www.codechef.com/problems/TIDRICE

codechef TIDRICE - "popular rice recipe" solution: http://ideone.com/5XLror

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

int main() {
int t, n, v[105], cnt;
char id[105][25], c;
scanf("%d", &t);
while(t--) {
scanf("%d", &n);
cnt=0;
for(int i=0; i<n; i++) {
scanf("%s %c", id[i], &c);
(c=='+') ?  v[i]=1 : v[i]=-1;
}
for(int i=n-1; i>0; i--) {
if(v[i]==0) continue;
for(int j=i-1; j>=0; j--) {
if(strcmp(id[i], id[j])==0) v[j]=0;
}
}
for(int i=0; i<n; i++) cnt += v[i];
printf("%d\n", cnt);
}
return 0;
}

codechef TIDRICE - "popular rice recipe" guidance: 

started from backwards, and looped through id array, if dublicate exist nullify dublicates vote(v[j]=0). and then count all votes and print it.

No comments:

Post a Comment