Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.math.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import java.util.concurrent.*;
- import java.util.function.*;
- import java.util.regex.*;
- import java.util.stream.*;
- import static java.util.stream.Collectors.joining;
- import static java.util.stream.Collectors.toList;
- import java.util.concurrent.atomic.AtomicInteger;
- public class Solution {
- public static void main(String[] args) throws IOException {
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
- int n = Integer.parseInt(bufferedReader.readLine().trim());
- List<String> genes = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
- .collect(toList());
- List<Integer> health = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
- .map(Integer::parseInt)
- .collect(toList());
- int s = Integer.parseInt(bufferedReader.readLine().trim());
- //Map<String, Integer> mappings = mapping(genes, health);
- AtomicInteger min = new AtomicInteger(Integer.MAX_VALUE);
- AtomicInteger max = new AtomicInteger(0);
- IntStream.range(0, s).forEach(sItr -> {
- try {
- String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
- int first = Integer.parseInt(firstMultipleInput[0]);
- int last = Integer.parseInt(firstMultipleInput[1]);
- String d = firstMultipleInput[2];
- int weight = weight(genes, health, first, last, d);
- if (weight < min.intValue()){
- min.set(weight);
- }
- if (weight > max.intValue()){
- max.set(weight);
- }
- } catch (IOException ex) {
- throw new RuntimeException(ex);
- }
- });
- bufferedReader.close();
- System.out.println(min + " " + max);
- }
- public static int geneOccurence(String strand, String gene){
- int result = 0;
- /*if (gene.length() == 1){
- char[] gChar = gene.toCharArray();
- return (int)strand.chars().filter(c -> c == gChar[0]).count();
- }*/
- int index = 0;
- while (true) {
- index = strand.indexOf(gene, index);
- if (index == -1){
- break;
- }
- result++;
- index++;
- }
- return result;
- }
- public static int weight(List<String> genes, List<Integer> health, int first, int last, String strand){
- /*
- *create wrapper class for health list
- *initilizes an IntStream with an intitial range between first and last
- *stream genes and set range between first and last
- *iterate over stream with forEach
- *retrieve first element of HealthWrapper intStream
- *remove first element of HealthWrapper intStream using skip()
- *
- */
- AtomicInteger result = new AtomicInteger(0);
- HealthWrapper healthWrapper = new HealthWrapper(health, first, last);
- genes.stream().skip(first).limit(last-first+1).forEach(gene ->{
- try {
- int occurence = geneOccurence(strand, gene);
- int weight = healthWrapper.get();
- healthWrapper.skip();
- result.addAndGet(weight*occurence);
- } catch (NullPointerException e){
- System.out.println(result);
- }
- });
- return result.intValue();
- }
- public static class HealthWrapper{
- IntStream healthStream;
- public HealthWrapper(List<Integer> health, int first, int last){
- healthStream = health.stream().skip(first).limit(last-first+1).mapToInt(Integer::intValue);
- }
- public int get(){
- return healthStream.findFirst().getAsInt();
- }
- public void skip(){
- healthStream = healthStream.skip(1);
- }
- }
- }
Add Comment
Please, Sign In to add comment