拳皇连招
从列表中找到录入的后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);
}
1.从列表中找到 以 XXX 结尾的所有选择 2. 结束条件 找到第一个可以使用的串 (当然原题 义不是这样的) 3.底归 找到所有可能性 4.选择最长路线 把所有可能性长度最大的那种保留下来 5.看一下是否大连招. 如果字串存在重复那么这个会一直循环下去.
每次变型都有简单的测试用例.
挨批了,,我。。 :? :? 怎么来说呢,个人感觉这个题目并不是很难,当然实现起来另说了。
我以为这个代码还是好懂的,其是我只是认为文字的东西少了点,要是能直接给人思路就好了。(我是不是太啥了点) 所以我认为里面有四个字“最大路径”能抵上所有的junit测试了。。。
算法都还给老师了。。。
代码不多. 就这么二十几行 想看直接看懂有点小难
但真的有心看 顺着我的例子抄一遍. 我想应该能看的懂.
说明真的一点都不少..... 楼上两人我不是真对你们俩.
某天,某程序员写了关于某个功能的代码,洋洋洒洒几千行,老板看到后说,小伙子,干得不错,这么短时间能够写出这么多代码。小伙子说,再给我一个月,我可以弄到几百行。老板脸黑了。。。 然后不久的一天,这个人被炒了....
楼主的代码我看懂了。。。。我根据图论也写了一个
http://leon-a.iteye.com/blog/779558
代码真没多少..... 大多数看到的是测试.....
楼主写的代码蛮好看的,就是说明太少了,看得累。。 :)