import java.io.PrintStream; import java.util.Arrays; import java.util.Scanner; /** * Solution to Letter Wheels */ public class wheels { /** The input. */ private static Scanner sc; /** The output. */ private static PrintStream ps; /** * Do it. */ private void doit() { // Read the strings char s1[] = sc.next().toCharArray(); char s2[] = sc.next().toCharArray(); String s3 = sc.next(); // This will be useful later on when we see how to rotate wheel 3 String s3s3 = s3+s3; // We can get n from the strings int n = s1.length; // ok[k] is true if we can rotate wheel 2 (left) by k and have mismatches at every position boolean ok[] = new boolean[n]; Arrays.fill( ok, true ); for( int i=0; i=0 ) { // We can always hold one of the wheels still // What if it's wheel 1? // We rotated wheel 2 by k and wheel 3 by p // if we rotated it left by x, then that's the same as rotating right by n-x // So, wee need to take the smaller of x and n-x int answer1 = Math.min( k, n-k ) + Math.min( p, n-p ); if( answer1n ? -1 : best ); } /** * The main method. * * @param args the arguments (unused) */ public static void main( String[] args ) { sc = new Scanner( System.in ); ps = System.out; new wheels().doit(); } }