import java.io.*; import java.util.*; import java.awt.geom.*; /** * Solution to Zig Zag Nametag * * @author vanb */ public class zigzag_vanb { public Scanner sc; public PrintStream ps; public String answer = null; public StringBuilder buffer = new StringBuilder(1000000); /** * Generate an answer string, specifying the last letter, * and whether to the next-to-last letter should go up or down. * Test to see if it's better than the best answer we've seen so far. * * @param k Sensei's favorite number * @param up Up or Down? * @param letter Last letter */ public void generateAnswer( int k, boolean up, char letter ) { buffer.setLength( 0 ); buffer.append( letter ); // Fill in the whole nametag while( k>0 ) { // diff is the distance up or down we've got to go // for the next letter int diff = Math.abs( (int)letter - (up?'z':'a')); if( k<=25 ) diff = k; else if( k<=50 ) diff = k/2; // The next letter char newletter = (char)(letter + (up?diff:-diff)); buffer.append( newletter ); letter = newletter; // It's possible that the next letter isn't a letter. // In that case, we fail. if( !Character.isLowerCase( letter ) ) break; // Prepare for the next iteration. // Swap directions, and remove diff from k. up = !up; k -= diff; } // If we found a solution if( k==0 ) { // Check to see if it's the best solution. String result = buffer.reverse().toString(); if( answer==null ) answer = result; else if( result.length()