111 Why WA??

All about problems in Volume I. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

111 Why WA??

Postby sumantbhardvaj » Fri Nov 10, 2006 4:22 pm

i just do not understand why my code is giving wrong answer.

I have applied LCS ( i think its more convinient to apply than LIS here)..

But recieving Wrong answer continously ..I have also taken care of the fact that the input gives the ranking of the events not the actual ordering..??

Can anyone explain where my code is lacking or provide some fresh test data??

Code: Select all
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
class sumant
{
public :
       int a[25];
       int b[25];
       int n,x;
       
       int c[25][25];
       
       void input()
       {
            cin>>n;
                       
            for(int i=1;i<=n;i++)
            {
                     cin>>x;
                     a[x]=i;
            }
            while(cin)
            {
                      for(int i=1;i<=n;i++)
                      {
                              cin>>x;
                              b[x]=i;
                      }
                      int z=doit();
                      cout<<z<<endl;
            }
       }     
       int doit()
       {
            for(int i=0;i<=n;i++)
            {
                    c[i][0]=0;
            }
            for(int i=0;i<=n;i++)
            {
                    c[0][i]=0;
            }
       
            for(int i=1;i<=n;i++)
            {
                    for(int j=1;j<=n;j++)
                    {
                            if(a[i]==b[j])
                            {
                                          c[i][j]=c[i-1][j-1]+1;
                            }
                            else if(c[i-1][j]>=c[i][j-1])
                            c[i][j]=c[i-1][j];
                            else
                            c[i][j]=c[i][j-1];
                    }
            }           
            return c[n][n];
        }
};
int main()
{
    sumant s;
    s.input();
    return 0;
}
sumantbhardvaj
New poster
 
Posts: 19
Joined: Sun Jun 18, 2006 4:07 pm

Postby Jan » Sat Nov 11, 2006 4:29 pm

Your code doesn't even pass the samples. Check it again. And dont open a new thread if there is one already.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Postby sumantbhardvaj » Sat Nov 11, 2006 8:18 pm

are u talking abt these test cases ( given in the question itself )

Code: Select all
10
3 1 2 4 9 5 10 6 8 7
1 2 3 4 5 6 7 8 9 10
4 7 2 3 10 6 9 1 5 8
3 1 2 4 9 5 10 6 8 7
2 10 1 3 8 4 9 5 7 6


then my program gives following output
Code: Select all
6
5
10
9

which matches with the one given .

can u be little more precise abt the error ??
sumantbhardvaj
New poster
 
Posts: 19
Joined: Sun Jun 18, 2006 4:07 pm

Postby Jan » Sat Nov 11, 2006 9:12 pm

My compiler is Ms-Vcc 6. I made an input file and ran your code. In my compiler your code returned
Code: Select all
6
5
10
9
9

For the above input.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Postby sumantbhardvaj » Sat Nov 11, 2006 11:19 pm

Thanx a lot..i got accepted finally :D :D

The problem was somewhere here ...

Code: Select all
while(cin)
            {
                      for(int i=1;i<=n;i++)
                      {
                              cin>>x;
                              b[x]=i;
                      }
                      int z=doit();
                      cout<<z<<endl;
            } 

i changed it a bit..

while(cin>>x) // for the first input and then the rest...

and it worked.... but i still could not understand that why my previous code printed the last value twice ...

Could u plz explain that...considering i m a novice coder at UVA :wink:
sumantbhardvaj
New poster
 
Posts: 19
Joined: Sun Jun 18, 2006 4:07 pm

Postby Jan » Sun Nov 12, 2006 1:22 pm

Ok let me explain the following part...

Code: Select all
            while(cin)
            {
                      for(int i=1;i<=n;i++)
                      {
                              cin>>x;
                              b[x]=i;
                      }
                      int z=doit();
                      cout<<z<<endl;
            }

After the last input, when 'while(cin)' line is executed it returns true. So execution continues. While 'cin>>x;' is executed it uses the last value which is 6. So, your code takes' 6 6 6 ... n times' and then the output becomes 9.
Hope you understand.
Ami ekhono shopno dekhi...
HomePage
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Postby andysoft » Mon Jul 23, 2007 12:28 pm

Well, I had this mistake with double printing last answer too, but I fixed it and still get WA.
Code: Select all
program Project2;

{$R-}{$S-}{$Q-}
{$APPTYPE CONSOLE}
type
  integer = longint;

var
  i,j,l,m,n: integer;
  a,b: array [1..30] of integer;
  c: array [0..30,0..30] of integer;
begin

  readln (n);
  for i:=1 to n do
  begin
    read (l);
    a[l] := i
  end;

  while not eof do
  begin
    for i:=1 to n do
    begin
      read (l);
      if (i<n) and eof then exit;
      b[l] := i;
    end;
    m := 0;
    for i:=0 to n+1 do
      for j:=0 to n+1 do
        c[i][j] := 0;

    for i:=2 to n+1 do
      for j:=2 to n+1 do
      begin
        if a[i-1]=b[j-1] then
          c[i][j] := c[i-1][j-1]+1
        else if (c[i-1][j]>=c[i][j-1]) then
          c[i][j] := c[i-1][j]
        else
          c[i][j] := c[i][j-1];
        if c[i][j] > m then
          m := c[i][j]
      end;

    writeln (m);

  end;

end.
Last edited by andysoft on Mon Aug 13, 2007 12:58 pm, edited 1 time in total.
Now I lay me down to sleep...
my profile
andysoft
Experienced poster
 
Posts: 109
Joined: Sat Jun 23, 2007 9:53 pm
Location: Brest, BELARUS

Postby andysoft » Mon Aug 13, 2007 11:18 am

Hi again!
As you, see I understood everything about the problem, but this WA is very-very annoying. Can anyone provide me test cases (probably tricky, but not connected with rank/order difference)??

Thanx in advance!!!
Now I lay me down to sleep...
my profile
andysoft
Experienced poster
 
Posts: 109
Joined: Sat Jun 23, 2007 9:53 pm
Location: Brest, BELARUS

Postby andysoft » Mon Aug 27, 2007 1:27 pm

Another hello to everyone!
I have finally got AC for this problem, but do you know how? You'll never guess. I have copied my pascal code to C++ editor, and command-by-command, operator-by-operator, variable-by-variable and so on I have translated my PAS code to PURE C code. When I have submitted, the first Judge verdict was ACCEPTED!!!
Unbeleivable!!!
And more shocking thing is that I had the same situation with 10038 task today!! : WA in PAS, but after translating to C/C++ it gave AC from the first time!!!!

HOW can it be? :) :o
Now I lay me down to sleep...
my profile
andysoft
Experienced poster
 
Posts: 109
Joined: Sat Jun 23, 2007 9:53 pm
Location: Brest, BELARUS

Postby Farnak » Thu Jan 17, 2008 12:37 am

Hi everyone,

I'm using the site http://icpcres.ecs.baylor.edu/onlinejudge/index.php, and when I browse the problem on their site I get:

Sample Output 2

6
5
10
9

But when I open the problem on pdf I get the following:

Sample Output 2

6
4
10
5

Which one is the correct output???
Farnak
New poster
 
Posts: 4
Joined: Wed May 24, 2006 9:15 pm
Location: Windsor, Canada

Postby Jan » Thu Jan 17, 2008 3:16 pm

The first one is correct.
Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Postby Farnak » Sat Jan 19, 2008 11:28 pm

Okay, I don't think I understand what the problem is really asking for then.

Sample Input:
10
3 1 2 4 9 5 10 6 8 7
1 2 3 4 5 6 7 8 9 10
4 7 2 3 10 6 9 1 5 8
3 1 2 4 9 5 10 6 8 7
2 10 1 3 8 4 9 5 7 6

I thought they wanted to know the length of the longest common subsequence between the first sequence and the following sequences, however in their sample output they give the answer "9" for the sequence "2 10 1 3 8 4 9 5 7 6" but there is definitely no subsequence of length 9 shared by that sequence and "3 1 2 4 9 5 10 6 8 7".

Could someone please clarify the problem statement for me?
Farnak
New poster
 
Posts: 4
Joined: Wed May 24, 2006 9:15 pm
Location: Windsor, Canada

Postby Jan » Sun Jan 20, 2008 2:04 pm

Jan
Guru
 
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh

Postby Farnak » Mon Jan 21, 2008 9:30 pm

Ah, thanks Jan! :D
I read the link and I got AC, I'll be more thorough with searching the forum next time.
Farnak
New poster
 
Posts: 4
Joined: Wed May 24, 2006 9:15 pm
Location: Windsor, Canada

Re: 111 Why WA??

Postby vinocit » Wed Nov 12, 2008 2:19 pm

Gr8 understanding Jan thanks :D
vinocit
New poster
 
Posts: 10
Joined: Mon Oct 13, 2008 10:11 am

Next

Return to Volume I

Who is online

Users browsing this forum: No registered users and 0 guests