-3

My first question on StackOverFlow.
This is the question: https://code.google.com/codejam/contest/11274486/dashboard#s=p0

I finished my c++ code and it worked normally, outputting file correctly (I think). But Dashboard system responded incorrectly. I don't know why it happened.
I hope somebody can figure out what's wrong with my algorithm, or share your code if you solved it. Thanks!!
And sorry for my poor English.
This is my code with Xcode 7.3.1:

// already solved the problem and removed my bad code :P

Thanks to Hagen von Eitzen,
Here is the right version:

#include <iostream>
using namespace std;

int letternum(string name)
{
    int i,num = 0;
    int count[26];
    for (i=0; i<26; i++)
        count[i] = 0;
    for (i=0; i<name.length(); i++)
    {
        if (name[i] != ' ')
            count[name[i]-'A'] = 1;
    }
    for (i=0; i<26; i++)
    {
        num += count[i];
    }
    return num;
}

int main()
{
    int T,N,t,n;
    int longlen,namelen;
    char tempchar[21];
    string longest;
    string name;

    FILE *fin  = fopen("A-large-practice.in", "r");
    FILE *fout = fopen("A-large-practice.out", "w");

    fscanf(fin, "%d", &T);

    for (t=0; t<T; t++)
    {
        memset(tempchar, '\0', 21);
        longest = "";
        name = "";
        fscanf(fin, "%d\n", &N);
        fscanf(fin, "%[^\n]%*c", tempchar);
        longest = tempchar;
        longlen = letternum(longest);

        for (n=1; n<N; n++)
        {
            memset(tempchar, '\0', 21);
            name = "";
            fscanf(fin, "%[^\n]%*c", tempchar);
            name = tempchar;
            namelen = letternum(name);
            if (longlen == namelen )
            {
                if (longest > name)
                {
                    longest = name;
                    longlen = letternum(longest);
                }
            }
            if ( longlen < namelen )
            {
                longest = name;
                longlen = letternum(longest);
            }
        }
        fprintf(fout, "Case #%d: %s\n", t+1, longest.c_str());
    }
}
ribilynn
  • 460
  • 5
  • 21

2 Answers2

0

Names may consist of up to 20 characters. You need to reserve space for the terminating NUL character as well. Otherwise, your code may crash if the input really is 20 chars long

letternum: Performance (hardly relevant here): You always count to the maximal length 20, which may be unnecessarily long; also, you are just lucky that you did not forget to zero the full array. If you replace the ++ with a =1 in the first loop, you need only sum in the second loop.

Also, why don't you use standards such as string or strcmp?

Hagen von Eitzen
  • 2,109
  • 21
  • 25
  • Thank you very much! I changed length of 'letternum' to 21 and it become correct! Also I change all char[] to string. But there is a trouble using fscanf to input to string (I believe it's my problem) so I add a temp char[]. As far as strcmp, I just forgot it... – ribilynn Jul 10 '16 at 12:42
  • @usako_lynn You may want to ead http://stackoverflow.com/questions/5455802/how-to-read-a-complete-line-from-the-user-using-cin about the C++ way – Hagen von Eitzen Jul 10 '16 at 14:33
0

this works fine for both small and large dataset.

def word(a):
''' this will find the total number of unique character in the name'''     
x=a.split()          
x="".join(x)
x=list(x)
x=set(x)
x=list(x)
return len(x)

with open('A-large-practice.in','r') as f:
  content = f.read().splitlines()
fo=open('large_output.txt','r+')
i=1
p=0
while i<len(content):
x=int(content[i])
y=[]
name=[]
for j in range(1,x+1):
    name.append(content[i+j])
    y.append(word(content[i+j]))
z=[name[i] for i in range(len(y)) if y[i]==max(y)]
z.sort()
z=z[0]
p=p+1
fo.write('case #{}: {} \n'.format(p,z))
i=i+x+1
fo.close()