482 - Permutation Arrays

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

Moderator: Board moderators

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

Re: 482 Permutation Arrays

Post by uDebug »

jddantes wrote:What's wrong with mine?
Try reading the thread more carefully.
There is a blank line after every set of output. However, as mentioned several times before, there is no blank line after the last set of output.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.
jddantes
Learning poster
Posts: 73
Joined: Sat Mar 08, 2014 8:55 am

Re: 482 Permutation Arrays

Post by jddantes »

Oh, sorry for that. And thanks too :)
lotus2bloom
New poster
Posts: 1
Joined: Sat Jul 26, 2014 10:03 am

Giving a wrong answer for PermutationArrays ID 482

Post by lotus2bloom »

import java.io.*;
import java.util.*;

class Main {

public static void main(String[] args) {

try
{
String line="";
line=Main.readLine(1024);
int numberOfTestCases=Integer.parseInt(line);
StringTokenizer st;
ArrayList<Integer> permutationStore=new ArrayList<Integer>();
ArrayList<Float> doubleStore=new ArrayList<Float>();
Float[] actualStore;

while(numberOfTestCases>0)
{
permutationStore.clear();
doubleStore.clear();
line=Main.readLine(1024);
line=Main.readLine(1024);
st=new StringTokenizer(line);
int tokens=st.countTokens();
for(int i=0;i<tokens;i++)
{
permutationStore.add(Integer.parseInt(st.nextToken())-1);
}

line=Main.readLine(1024);
st=new StringTokenizer(line);
for(int i=0;i<tokens;i++)
{
doubleStore.add(Float.parseFloat(st.nextToken()));
}

actualStore=new Float[permutationStore.size()];
for(int i=0;i<permutationStore.size();i++)
{
actualStore[permutationStore.get(i)]=doubleStore.get(i);
}

for(int i=0;i<actualStore.length;i++)
{
System.out.println(actualStore);
}

numberOfTestCases--;

}


}
catch(Exception e)
{
e.printStackTrace();
}
}

static String readLine(int sizeOfBuffer)
{
byte[] inputAsBytes=new byte[sizeOfBuffer];
int inputEndCheck=-1;
int currentBytePosition=0;

try
{
while(currentBytePosition<sizeOfBuffer)
{
inputEndCheck=System.in.read();
if(inputEndCheck<0 || inputEndCheck=='\n')
{break; }
inputAsBytes[currentBytePosition++]+=inputEndCheck;

}

}
catch(IOException e)
{
e.printStackTrace();
}

if((currentBytePosition==0)&&(inputEndCheck<0))
return null;
return new String(inputAsBytes,0,currentBytePosition);
}

}


Can anyone kindly tell me what is wrong with the code ? It is running correctly in my compiler...
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: Giving a wrong answer for PermutationArrays ID 482

Post by brianfry713 »

Check input and AC output for thousands of problems on uDebug!
cyberdragon
New poster
Posts: 20
Joined: Fri Aug 30, 2013 5:42 am

482 - Permutation Arrays

Post by cyberdragon »

Why If we used a map of <string, string> we get WA but when we use map<int, string> we get AC?
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 482 - Permutation Arrays

Post by brianfry713 »

I solved it using an array of pair<int, string>, so I'm not sure what your approach is, but I'm guessing you're asking why you can't sort as strings instead of ints.
Try comparing 10 and 2 as ints and as strings. 10 > 2 as ints, "10" < "2" as strings.
Check input and AC output for thousands of problems on uDebug!
mauricioco
New poster
Posts: 1
Joined: Tue Apr 07, 2015 4:44 pm

Re: 482 - Permutation Arrays

Post by mauricioco »

Can someone tell me why this isn't working? It's probably the way I'm reading the input, but I couldn't find a counter-example. Everything I threw at it worked. But it's still considered wrong, according to uva online judge.

Code: Select all

#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<stack>
#include<map>
#include<string>
#include<cstring>

using namespace std;

int main() {
	int c, i, f;
	scanf("%d", &c);
	for(i=0; i<c; i++) {
		if(i) printf("\n");
		vector<pair<long long, string> > v;
		long long index;
		char value[100000];
		char w[1024];
		while(scanf("%lld%[^0-9]", &index, w) > 0) {
			v.push_back(make_pair(index, ""));
			int len = strlen(w);
			if(w[len-1] == '\n')
				break;
		}
		
		int j;
		for(j=0; j<v.size(); j++) {
			scanf("%s", value, w);
			string str(value);
			v[j].second = str;
			int len = strlen(w);
			if(w[len-1] == '\n')
				break;
		}
		
		sort(v.begin(), v.end());
		
		vector<pair<long long, string> >::iterator k;
		for(k = v.begin(); k != v.end(); k++) {
			printf("%s\n", (k->second).c_str());
		}
	}
	
	return 0;
}
AnishS
New poster
Posts: 1
Joined: Thu Dec 03, 2015 3:40 am

Re: 482 - Permutation Arrays

Post by AnishS »

For some reason, I keep getting WA on this problem. I checked against all inputs I could think of and some of my friends do not know what I am doing wrong. Can someone please help?

Code: Select all

import java.io.*;
import java.util.*;

public class UVA482 {
	public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(bf.readLine());
        for (int i = 0; i < N; i++){
        	bf.readLine();
        	String[] places = bf.readLine().trim().split(" ");
        	int[] place = new int[places.length];
        	for (int j = 0; j < places.length; j++){
        		place[j] = places[j].charAt(0)-'1';
        	}
        	
        	String[] numbers = new String[places.length];
        	StringTokenizer st = new StringTokenizer(bf.readLine().trim());
        	for (int j = 0; j < places.length;j++){
        		numbers[j] = st.nextToken();
        	}
        	String [] newnum = new String[places.length];
        	for (int j = 0; j < places.length; j++){
        		newnum[place[j]] = numbers[j]; 
        	}
        	
        	for (String f: newnum){
        		System.out.println(f);
        	}
        	
        	if (i < N-1) System.out.println();
        }
	}
}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 482 - Permutation Arrays

Post by brianfry713 »

use class Main
Check input and AC output for thousands of problems on uDebug!
obelisk
New poster
Posts: 1
Joined: Mon Jun 27, 2016 7:24 pm

Re: 482 - Permutation Arrays

Post by obelisk »

My code below gives WA and I'm sure that code works but the output format is the issue can someone help me be sure what is going wrong.
Even debug shows identical output with accepted output

Code: Select all


import static java.lang.Integer.parseInt;
import static java.lang.System.exit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;

 class Main{

	static void solve() throws IOException {
		
		int nrTest = nextInt();
		while(nrTest-- > 0){
			in.readLine();
			String [] index = in.readLine().split(" ");
			String [] elements = in.readLine().split(" ");
			String [] result = new String[elements.length];
			
			for (int i = 0; i < result.length; i++) {
				result[Integer.parseInt(index[i])-1] = elements[i];
			}
			
			for (int i = 0; i < result.length; i++) {
				System.out.printf("%s\n",result[i]);
			}
			System.out.println();
		}
	}

	static int nextInt() throws IOException {
		return parseInt(next());
	}

	static String next() throws IOException {
		while (tok == null || !tok.hasMoreTokens()) {
			tok = new StringTokenizer(in.readLine());
		}
		return tok.nextToken();
	}

	public static void main(String[] args) {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(new OutputStreamWriter(System.out));
			solve();
			in.close();
			out.close();
		} catch (IOException e){
			e.printStackTrace();
			exit(0);
		}
		
	}

	static BufferedReader in;
	static PrintWriter out;
	static StringTokenizer tok;

}
Post Reply

Return to “Volume 4 (400-499)”