1. 程式人生 > >順豐科技 2019校招線上考試

順豐科技 2019校招線上考試

第一題:

求出匹配的迴文字串字串

例如

輸入:AAABCCCDEEE

輸出:AAABCCC

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        String s1 = longestPalindrome(input);
        String s2 = longestPalindrome1(input,input.indexOf(s1)+ s1.length());
        System.out.println(s1);
        System.out.println(s2);
        if (s1!=null&&s2!=null&&s1.length()==s2.length()&&s2.length()<input.length()){
            System.out.println(input.substring(input.indexOf(s1),input.indexOf(s2))+s2);
        }else {
            System.out.println("NULL");
        }

    }
    public  static String longestPalindrome1(String s,int length) {
        if (s.isEmpty()) {
            return null;
        }
        if (s.length() == 1) {
            return s;
        }
        String longest = s.substring(0, 1);
        for (int i = length; i <s.length(); i++) {
            // get longest palindrome with center of i
            String tmp = helper(s, i, i);
            if (tmp.length() > longest.length()) {
                longest = tmp;
            }

            // get longest palindrome with center of i, i+1
            tmp = helper(s, i, i + 1);
            if (tmp.length() > longest.length()) {
                longest = tmp;
            }
        }
        return longest;
    }
    public  static String longestPalindrome(String s) {
        if (s.isEmpty()) {
            return null;
        }
        if (s.length() == 1) {
            return s;
        }
        String longest = s.substring(0, 1);
        for (int i=0; i < s.length(); i++) {
            // get longest palindrome with center of i
            String tmp = helper(s, i, i);
            if (tmp.length() > longest.length()) {
                longest = tmp;
            }

            // get longest palindrome with center of i, i+1
            tmp = helper(s, i, i + 1);
            if (tmp.length() > longest.length()) {
                longest = tmp;
            }
        }
        return longest;
    }

    // Given a center, either one letter or two letter,
    // Find longest palindrome
    public static String helper(String s, int begin, int end) {
        while (begin >= 0 && end <= s.length() - 1
                && s.charAt(begin) == s.charAt(end)) {
            begin--;
            end++;
        }
        String subS = s.substring(begin + 1, end);
        return subS;
    }
};

第二題:

求出符合fullJustify這樣的字串

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.next();
        int L = sc.nextInt();
        String[] words = input.split(",");
        ArrayList<String> strings = fullJustify(words, L);
        for (String s :strings){
            System.out.println(s);
        }
    }
    public static ArrayList<String> fullJustify(String[] words, int L) {
        ArrayList<String> res = new ArrayList<String>();
        if(words==null || words.length==0)
            return res;
        int count = 0;
        int last = 0;
        for(int i=0;i<words.length;i++)
        {
            if(count+words[i].length()+(i-last)>L)
            {
                int spaceNum = 0;
                int extraNum = 0;
                if(i-last-1>0)
                {
                    spaceNum = (L-count)/(i-last-1);
                    extraNum = (L-count)%(i-last-1);
                }
                StringBuilder str = new StringBuilder();
                for(int j=last;j<i;j++)
                {
                    str.append(words[j]);
                    if(j<i-1)
                    {
                        for(int k=0;k<spaceNum;k++)
                        {
                            str.append(" ");
                        }
                        if(extraNum>0)
                        {
                            str.append(" ");
                        }
                        extraNum--;
                    }
                }
                for(int j=str.length();j<L;j++)
                {
                    str.append(" ");
                }
                res.add(str.toString());
                count=0;
                last=i;
            }
            count += words[i].length();
        }
        StringBuilder str = new StringBuilder();
        for(int i=last;i<words.length;i++)
        {
            str.append(words[i]);
            if(str.length()<L)
                str.append(" ");
        }
        for(int i=str.length();i<L;i++)
        {
            str.append(" ");
        }
        res.add(str.toString());
        return res;
    }


}