TECH04: Getting wa, need help

http://www.codechef.com/problems/TECH04

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

int main(){
    int t;
    bool possible=true;
    scanf("%d",&t,"\n");
    for(int i=1; i<=t; i++){
            char worda[30],wordb[30];
            scanf("%s %s",worda,wordb,"\n");
            string stringa,stringb;
            stringa = worda; stringb = wordb;
            for(int j=0; j<=(stringa.length()-1); j++){
                    char temp;
                    temp = stringa[j];
                    if (temp == '.') {continue;}
                    int counta = 1;
                    int countb = 0;
                       for (int k=j+1; k<=(stringa.length()-1); k++){
                           if (temp==stringa[k]) {counta++;
                                             stringa[k]='.';}}
                       for (int k=0; k<=(stringb.length()-1); k++){
                           if (temp==stringb[k]) {countb++;}}
                     if (counta==countb) {possible = true;} 
                       else {possible = false;
                            break;}
                    }
            if (possible) {printf("YES\n");}
               else {printf("NO\n");}
            }
    return 0;
}

This is my code for problem Anagram in Peer section. I’m getting Wrong answer. Please, tell me what’s wrong here since all my test cases go well, and there might be some big error because I’m pretty new in C++.
Thanks :slight_smile:

even if it does not answer your question, i got AC with a VERY simple code. the shorter your code is, the less you’re likely to make mistakes. :slight_smile:

1 Like

Yeah, you can just sort string in alphabetical order and check if the strings are equal, mine code is too much simpler than yours http://www.codechef.com/viewsolution/1004949 :slight_smile:

1 Like

Well, the main purpose of me solving this problem is to improve my C++ because I want to make transition from Pascal to C++ and I wanted to check whether each string has the SAME NUMBER of the SAME LETTERS… I can’t see where I go wrong :slight_smile: and this idea of sorting characters in words is smart, but it’s not acceptable for C++ training since it’s very simple.

If you can find any error please reply to this post, anyway thank you all guys for answers :slight_smile:

i found it ! :slight_smile:

1
a ab

oh, i forgot something.

if you really want to do C++, please do not use scanf and printf, but std::cin and std::cout, like that :

int t; string stringa, stringb;

cin >> t;
while (t--)
{
    cin >> stringa >> stringb;
    [...]
    cout << (possible ? "YES" : "NO") << endl;
}

it is far easier to understand in a C++ code. otherwise, you could simply write C code.

If you want to learn C++, you can be interested in map (http://en.cppreference.com/w/cpp/container/map). You can see Java implementation using map (not the same problem) on CodeForces (http://codeforces.com/blog/entry/3976), there are links for other problems where u can use this :wink: