神州数码笔试题目

/**
 * 神州数码笔试题目
 * 原题:1.找出两个字符串中最大子字符串,如"abractyeyt","dgdsaeactyey"的最大子串为"actyet"
 * 语言:实现语言  java
 *
 
*/


import  java.util. * ;

public   class  SubString  {

    ArrayList ls 
= new ArrayList();

    
public void getSameString(String s1,String s2){
        String s3 
="";//is the min string        
        String s4 ="";//is the max string
        if(s1.length() > s2.length()){
            s3 
= s2;
            s4 
= s1;    
        }
else{
            s3 
= s1;
            s4 
= s2;    
        }

        String s 
= "";//存储最后得到的字符串
        int len = s3.length();//is min length of the string
        for(int i=0;i<len;i++){
            
int m = 0;
            
int ch = (int)s3.charAt(i);    
            
int ch1 = (int)s4.charAt(m);
            
            
if(ch == ch1){
                
for(int j=i;j<len;j++){
                    ch 
= (int)s3.charAt(j);    
                    ch1 
= (int)s4.charAt(m);
                    
if(ch1 == ch){
                        s 
= s + String.valueOf(s3.charAt(j));    
                    }
else{
                        
if(!s.equals(""))
                            ls.add(s);
                        s 
= "";
                        
break;    
                    }

                    m
++;
                }
//end for
                if(!s.equals(""))
                    ls.add(s);
                s 
= "";
            }
else{
                
//循环找到一个首字母相同    
                while(m < s4.length()){
                    ch1 
= (int)s4.charAt(m);
                    ch 
= (int)s3.charAt(i);//重要,不要叫下面的给覆盖    
                    if(ch == ch1){
                        
for(int j=i;j<len&&m<s4.length();j++){
                            ch 
= (int)s3.charAt(j);
                            ch1 
= (int)s4.charAt(m);    
                            
//System.out.println("ch : "+s3.charAt(j) + " : "+s4.charAt(m));
                            if(ch == ch1){
                                s 
= s + String.valueOf(s3.charAt(j));    
                            }
else{
                                
if(!s.equals(""))
                                    ls.add(s);
                                s 
= "";
                                m
--;
                                
break;
                            }

                            m
++;
                        }
//end inner for
                        if(!s.equals(""))
                            ls.add(s);
                        s 
= "";
                    }
else{
                        m
++;
                        
continue;    
                    }

                    m
++;
                }
//end while
            }
//end else
        }

    }

    
    
public String getMaxSameString(String s1,String s2){
        getSameString(s1,s2);
        String s 
= "";
        
int len = 0;
        
for(int i=0;i<ls.size();i++){
            String sub 
= (String)ls.get(i);
            
if(sub.length() > len){
                s 
= sub;
                len 
= sub.length();    
            }

        }

        
return s;    
    }

    
    
public static void main(String[] args){
        SubString sub 
= new SubString();
        
if(args.length < 2){
            System.out.println(
"please input the string");    
            
return;
        }

        System.out.println(sub.getMaxSameString(args[
0],args[1]));
    }

}

// 更多题目见:www.search.bdkey.com
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值