Created
February 11, 2016 02:02
-
-
Save ItaloQualisoni/e1325f220038c86668b8 to your computer and use it in GitHub Desktop.
Possible Solution BuildString
This file contains hidden or 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.*; | |
import java.util.*; | |
import java.text.*; | |
import java.math.*; | |
import java.util.regex.*; | |
import java.lang.*; | |
import java.util.Arrays; | |
public class Solution { | |
private static boolean debug = false; | |
private static boolean debugSteps = false; | |
private static boolean debugTeste1 = false; | |
private static boolean debugCount = false; | |
public static void main(String[] args) { | |
Scanner in = new Scanner(System.in); | |
int t = in.nextInt(); | |
in.nextLine(); | |
while(t > 0){ | |
String line = in.nextLine(); | |
int n = Integer.parseInt(line.split(" ")[0]); | |
int a = Integer.parseInt(line.split(" ")[1]); | |
int b = Integer.parseInt(line.split(" ")[2]); | |
String word = in.nextLine(); | |
if(debugSteps){ | |
System.out.println("#############################"); | |
System.out.println("line: "+line); | |
System.out.println("a: "+a+" b: "+b + " n: "+n); | |
System.out.println("word: "+word); | |
System.out.println("#############################"); | |
} | |
System.out.println(solve(n,a,b,word)); | |
t--; | |
} | |
} | |
public static int solve(int n , int a , int b , String word){ | |
int quantidadeChar = b/a; | |
if(quantidadeChar == 1 && a < b){ | |
quantidadeChar++; | |
} | |
int total = 0; | |
String desired = ""; | |
if(debug){ | |
System.out.println( "Quantidade de char para valer append substring: " + quantidadeChar); | |
System.out.println("Initial : "+desired + " Final " + word); | |
} | |
while(!desired.equals(word)){ | |
String subString = ""; | |
int possiblePrice = 0; | |
if(desired.isEmpty() || desired.length() + quantidadeChar > word.length()){ | |
subString = word.charAt(desired.length()) + ""; | |
possiblePrice = a; | |
} | |
else{ | |
String restOfWord = word.substring(desired.length(),word.length()); | |
String sub = ""; | |
if(debug){ | |
System.out.println("restOfWord : " + restOfWord + ";"+restOfWord.length()); | |
} | |
//all substrings of desired | |
for( int i = quantidadeChar ; i <= restOfWord.length() ; i++ ){ | |
String possibleSubString = restOfWord.substring(0, i); | |
if(debug){ | |
System.out.println("Testando Substring : " + possibleSubString); | |
} | |
boolean contains = desired.contains(possibleSubString); | |
if(!contains){ | |
if(subString.length() >= 2 && a < b){ | |
String test = possibleSubString.substring(1, possibleSubString.length()); | |
if(debugSteps){ | |
System.out.println("possibleSubString : " + possibleSubString); | |
System.out.println("Testando Substring para ver se vale append : " + test); | |
} | |
contains = desired.contains(test); | |
if(contains){ | |
subString = possibleSubString.substring(0,1);; | |
possiblePrice = a; | |
} | |
} | |
break; | |
} | |
else if(possibleSubString.length() >= quantidadeChar){ | |
if(subString.length() <= possibleSubString.length()){ | |
//Copy and append: bc | |
subString = possibleSubString; | |
possiblePrice = b; | |
} | |
} | |
} | |
} | |
if(possiblePrice == 0){ | |
if(debug){ | |
System.out.println("possiblePrice zerado"); | |
} | |
subString = word.charAt(desired.length()) + ""; | |
possiblePrice = a; | |
} | |
desired += subString; | |
total += possiblePrice; | |
if(debugSteps){ | |
System.out.print("Cost:" + possiblePrice + " "); | |
if(possiblePrice == a) | |
System.out.print("Append: "+subString); | |
else{ | |
System.out.print("Copy and append: "+subString); | |
} | |
System.out.println("\t[" +desired+"]"); | |
} | |
if(debugCount){ | |
System.out.print(possiblePrice + " + "); | |
} | |
} | |
return total; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment