Last active
August 29, 2015 14:06
-
-
Save kazurof/f103f7ec1c845c99100b to your computer and use it in GitHub Desktop.
sample code for https://paiza.jp/poh/kirishima with awk over Java.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.io.OutputStream; | |
public class Main { | |
public static void main(String[] args) throws Exception { | |
StringBuilder script = new StringBuilder(); | |
script.append("BEGIN {"); | |
script.append(" FS = \" \";"); | |
script.append(" requiedNumOfMember = 0;"); | |
script.append(" numOfCompany = 0;"); | |
script.append("}"); | |
script.append("NF == 1 {"); | |
script.append(" if(requiedNumOfMember == 0){"); | |
script.append(" requiedNumOfMember = $1"); | |
script.append(" } else {"); | |
script.append(" numOfCompany = $1"); | |
script.append(" }"); | |
script.append("}"); | |
script.append("NF == 2 {"); | |
script.append(" membersOfCompany[FNR - 3] = $1;"); | |
script.append(" costOfCompany[FNR - 3] = $2;"); | |
script.append(" numOfMemberIfUseAll += $1;"); | |
script.append("}"); | |
script.append("END {"); | |
script.append(" leastCostByNumOfMember[0] = 0;"); | |
script.append(" for (i = 1; i < numOfMemberIfUseAll + 1 ; i++) {"); | |
script.append(" leastCostByNumOfMember[i] = -1;"); | |
script.append(" }"); | |
script.append(" for (i = 0; i < numOfCompany; i++) {"); | |
script.append(" for (j = numOfMemberIfUseAll; j >= membersOfCompany[i]; j--) {"); | |
script.append(" costWithoutCompanyJ = leastCostByNumOfMember[j - membersOfCompany[i]];"); | |
script.append(" if (costWithoutCompanyJ >= 0) {"); | |
script.append(" newCost = costWithoutCompanyJ + costOfCompany[i];"); | |
script.append(" if (leastCostByNumOfMember[j] < 0 || leastCostByNumOfMember[j] > newCost) {"); | |
script.append(" leastCostByNumOfMember[j] = newCost;"); | |
script.append(" }"); | |
script.append(" }"); | |
script.append(" }"); | |
script.append(" }"); | |
script.append(" j = -1;"); | |
script.append(" for (i = requiedNumOfMember; i <= numOfMemberIfUseAll; i++) {"); | |
script.append(" if (j < 0 || (leastCostByNumOfMember[i] >= 0 && j > leastCostByNumOfMember[i])) {"); | |
script.append(" j = leastCostByNumOfMember[i];"); | |
script.append(" }"); | |
script.append(" }"); | |
script.append(" print j;"); | |
script.append("}"); | |
ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "awk '" + script.toString() + "'"); | |
pb.redirectErrorStream(true); | |
Process process = pb.start(); | |
passInputToOutput(System.in, process.getOutputStream()); | |
process.waitFor(); | |
printInputStream(process.getInputStream()); | |
process.waitFor(); | |
} | |
static void passInputToOutput(InputStream in, OutputStream out) throws IOException { | |
byte[] buffer = new byte[1024]; | |
int len = in.read(buffer); | |
while (len != -1) { | |
out.write(buffer, 0, len); | |
len = in.read(buffer); | |
} | |
out.close(); | |
} | |
static void printInputStream(InputStream is) throws IOException { | |
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) { | |
while (true) { | |
String line = br.readLine(); | |
if (line == null) | |
break; | |
System.out.println(line); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment