Page 11 of 11

Re: 482 Permutation Arrays

Posted: Sat Mar 08, 2014 9:32 am
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.

Re: 482 Permutation Arrays

Posted: Tue Mar 11, 2014 7:13 pm
by jddantes
Oh, sorry for that. And thanks too :)

Giving a wrong answer for PermutationArrays ID 482

Posted: Sat Jul 26, 2014 10:08 am
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...

Re: Giving a wrong answer for PermutationArrays ID 482

Posted: Mon Jul 28, 2014 7:48 pm
by brianfry713

482 - Permutation Arrays

Posted: Wed Jul 30, 2014 7:55 am
by cyberdragon
Why If we used a map of <string, string> we get WA but when we use map<int, string> we get AC?

Re: 482 - Permutation Arrays

Posted: Wed Jul 30, 2014 7:45 pm
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.

Re: 482 - Permutation Arrays

Posted: Tue Apr 07, 2015 4:49 pm
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;
}

Re: 482 - Permutation Arrays

Posted: Thu Dec 03, 2015 3:43 am
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();
        }
	}
}


Re: 482 - Permutation Arrays

Posted: Tue Dec 08, 2015 9:04 pm
by brianfry713
use class Main

Re: 482 - Permutation Arrays

Posted: Tue Jun 28, 2016 4:16 am
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;

}