欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

拳皇连招 博客分类: 技术 算法junit 

程序员文章站 2024-02-04 12:39:04
...
从列表中找到录入的后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);
	}
相关标签: 算法 junit