拳皇連招


リストから入力後Nビットを見つける

class KOFTest {
	@Test
	public final void testKoofFindOne() {
		KOF kof = new KOF(Arrays.asList("ABCD"));
		String koof = kof.koof("ABC");
		Assert.assertEquals("ABCD", koof);
	}
}


public class KOF {
	private static final int KEYLEN = 3;
	static List<String> list = null ;
	
	public KOF(List<String> asList) {
		this.list = asList;
	}

	public String koof(String str){
		for(String s : list){
//			System.out.println(str.substring(str.length()-3));
//			System.out.println(s.startsWith("ABC"));
			if(s.startsWith(str.substring(str.length()-KEYLEN))){
				return s;
			}			
		}

		
		return "";
	}

}


終了条件
最初に使用できる列を見つけます
	
	@Test
	public final void testKoofFindNone() {
		KOF kof = new KOF(Arrays.asList("ABCD"));
		String koof = kof.koof("ABE");
		Assert.assertEquals("ABE", koof);
	}
	@Test
	public final void testKoofFindOne() {
		KOF kof = new KOF(Arrays.asList("ABCD"));
		String koof = kof.koof("ABC");
		Assert.assertEquals("ABCD", koof);
	}
	@Test
	public final void testKoofFindTwo() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDF"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDF", koof);
	}

	public String koof(String str){
		List<String> select = new ArrayList<String>();
		for(String s : list){
			System.out.println(str.substring(str.length()-3));
			if(s.startsWith(str.substring(str.length()-3))){
				select.add(s);
			}			
		}
		System.out.println(select);		
		for(String s  : select){
			return str+s.substring(s.length()-1);
			
		}
		
		return str;
	}
}



底帰
	@Test
	public final void testKoofFindNone() {
		KOF kof = new KOF(Arrays.asList("ABCD"));
		String koof = kof.koof("ABE");
		Assert.assertEquals("ABE", koof);
	}
	@Test
	public final void testKoofFindOne() {
		KOF kof = new KOF(Arrays.asList("ABCD"));
		String koof = kof.koof("ABC");
		Assert.assertEquals("ABCD", koof);
	}
	@Test
	public final void testKoofFindTwo() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDF"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDF", koof);
	}
	
	@Test
	public final void testKoofFindTree() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDE","CDEF"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDEF", koof);
	}

	public String koof(String str){
		List<String> select = new ArrayList<String>();
		for(String s : list){
			System.out.println(str.substring(str.length()-3));
			if(s.startsWith(str.substring(str.length()-3))){
				select.add(s);
			}			
		}
		for(String s  : select){
			return koof(str+s.substring(s.length()-1));
			
		}		
		return str;
	}


最長ルートを選択

	@Test
	public final void testKoofFindNone() {
		KOF kof = new KOF(Arrays.asList("ABCD"));
		String koof = kof.koof("ABE");
		Assert.assertEquals("ABE", koof);
	}
	@Test
	public final void testKoofFindOne() {
		KOF kof = new KOF(Arrays.asList("ABCD"));
		String koof = kof.koof("ABC");
		Assert.assertEquals("ABCD", koof);
	}
	@Test
	public final void testKoofFindTwo() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDF"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDF", koof);
	}
	
	@Test
	public final void testKoofFindThree() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDE","CDEF"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDEF", koof);
	}
	
	@Test
	public final void testKoofFindTree() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDF","BCDE","CDEF"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDEF", koof);
	}
	public String koof(String str){
		List<String> select = new ArrayList<String>();
		for(String s : list){
			System.out.println(str.substring(str.length()-3));
			if(s.startsWith(str.substring(str.length()-3))){
				select.add(s);
			}			
		}
		String max = str;
		for(String s  : select){
			String temp =  koof(str+s.substring(s.length()-1));
			if(max.length()<temp.length()){
				max=temp;
			}
			
		}		
		return max;
	}

大連招かどうか見てみましょう.
public String koof(String str){
		List<String> select = new ArrayList<String>();
		if(str.endsWith("EOF")){
			return str;
		}
		for(String s : list){
			if(s.startsWith(str.substring(str.length()-3))){
				select.add(s);
			}			
		}
		String max = str;
		for(String s  : select){
			String nextKey = str+s.substring(s.length()-1);
			if(str.indexOf(s.substring(1))>=0){
				return str+"EOF";
			}
			String temp =  koof(nextKey);
			if(max.length()<temp.length()){
				max=temp;
			}
			
		}		
		return max;
	}
	@Test
	public final void testKoofFindNone() {
		KOF kof = new KOF(Arrays.asList("ABCD"));
		String koof = kof.koof("ABE");
		Assert.assertEquals("ABE", koof);
	}
	@Test
	public final void testKoofFindOne() {
		KOF kof = new KOF(Arrays.asList("ABCD"));
		String koof = kof.koof("ABC");
		Assert.assertEquals("ABCD", koof);
	}
	@Test
	public final void testKoofFindTwo() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDF"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDF", koof);
	}
	
	@Test
	public final void testKoofFindThree() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDE","CDEF"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDEF", koof);
	}
	
	@Test
	public final void testKoofFindTree() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDF","BCDE","CDEF"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDEF", koof);
	}
	
	@Test
	public final void testKoofFindRound() {
		KOF kof = new KOF(Arrays.asList("ABCD","BCDA","CDAB","DABC"));
		String koof = kof.koof("ABCD");
		Assert.assertEquals("ABCDABEOF", koof);
	}