2016腾讯实习生在线笔试的两道编程题
2016-04-05 00:00
483 查看
我的github page:http://joshshaw.github.io/
再一次判断是否可以走,可以则走,不可以说明已经全部走完,程序结束。
于是在代码中最主要的方法为
<!-- more -->
长度为len,
最大回文子串的长度为maxLen[i][j],i为该字符串的最左下标,j为最右下标。
那么maxLen[0][len-1]为最终解。
并且有状态转移方程:
上述的状态转移方程说明:
每个字符本身就是一个回文串,那么长度为1
当前后两个字符相等时,其最大长度就是2加上除去这两个字符所包含的子串的最大回文长度
在情况2的基础上,如果这两个字符是相邻的,那么最大长度就是2
当前后两个字符不相等,那么其最大长度由舍去前面字符的最大长度和舍去最后字符的最大长度中的最大值决定。
拥有状态转移方程,当然可以用动态规划来做,就是代码编写的难度比较大,其外使用递归的方法实现是简单易懂,缺点是
嵌套递归过多,重复子过多。
References & Thx:http://blog.csdn.net/eclipse88/article/details/6473804(里面的代码有点问题,不过整体思路是一样的,十分感谢)
0x00第一题(回形矩阵)
0x01描述
输入矩阵的边长,如5,然后生成如下的回形矩阵,最后一行行地输出。1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9
0x02思路
定义上下左右四个方向,递增地往当前方向走,判断越界或者是否已经被占用,是则向右走,再一次判断是否可以走,可以则走,不可以说明已经全部走完,程序结束。
于是在代码中最主要的方法为
boolean hasNext(),同时对越界的判断要仔细。
<!-- more -->
0x03代码
import java.util.Scanner; import static java.lang.System.in; import static java.lang.System.out; public class Main { enum Direction { UP, DOWN, LEFT, RIGHT } private Direction direction = Direction.RIGHT; private int o[][] = null; private int n; private int i=0, j=-1; public Main(int n) { this.n = n; o = new int ; } private boolean hasNext() { switch(direction){ case RIGHT: ++j; if(j>=n || o[i][j]!=0){ direction = Direction.DOWN; --j; ++i; } break; case DOWN: ++i; if(i>=n || o[i][j]!=0){ direction = Direction.LEFT; --i; --j; } break; case LEFT: --j; if(j<0 || o[i][j]!=0){ direction = Direction.UP; ++j; --i; } break; case UP: --i; if(i<0 || o[i][j]!=0){ direction = Direction.RIGHT; ++i; ++j; } break; } if(o[i][j]!=0)return false; else return true; } private void next(int put) {o[i][j]=put;} public void init() { int t = 1; while(hasNext()) next(t++); } public void print() { for(int x=0; x<n; ++x) for(int y=0; y<n; ++y) out.print(""+o[x][y]+" "); } public static void main(String[] args) { Scanner scanner = new Scanner(in); int input = scanner.nextInt(); Main m = new Main(input); m.init(); m.print(); } }
0x10第二题(求最长回文串长度)
0x11描述
找出一个字符串中的最大回文子串的长度。子串不需要连续。input: xafedgkdfe output: 5 reason: fd(g/k)df
0x12思路
设该字符串为str,长度为len,
最大回文子串的长度为maxLen[i][j],i为该字符串的最左下标,j为最右下标。
那么maxLen[0][len-1]为最终解。
并且有状态转移方程:
| 1 str[i]==str[j], i==j maxLen[i][j] = | 2 + maxLen[i+1][j-1] str[i]==str[j], i>j且i+1>j-1 | 2 str[i]==str[j], i+1<j-1 | max( maxLen[i+1][j], maxLen[i][j-1] ) str[i]!=str[j]
上述的状态转移方程说明:
每个字符本身就是一个回文串,那么长度为1
当前后两个字符相等时,其最大长度就是2加上除去这两个字符所包含的子串的最大回文长度
在情况2的基础上,如果这两个字符是相邻的,那么最大长度就是2
当前后两个字符不相等,那么其最大长度由舍去前面字符的最大长度和舍去最后字符的最大长度中的最大值决定。
拥有状态转移方程,当然可以用动态规划来做,就是代码编写的难度比较大,其外使用递归的方法实现是简单易懂,缺点是
嵌套递归过多,重复子过多。
0x13代码
0x14递归实现
import java.util.Scanner; public class M { static int MaxLength(char str[], int i, int j) { if (i == j) return 1; else if (i > j) return 0; else { if (str[i] == str[j]) { return 2 + MaxLength(str, i + 1, j - 1); } else { return Math.max(MaxLength(str, i + 1, j), MaxLength(str, i, j - 1)); } } } public static void main(String[] args) { Scanner s = new Scanner(System.in); char str[]; str = s.nextLine().toCharArray(); System.out.println( MaxLength(str, 0, str.length-1) ); } }
0x15动态规划
import java.util.Scanner; public class M2 { private char str[]; private int slen; private int maxlen[][]; public M2(String string) { slen = string.length(); str = string.toCharArray(); maxlen = new int [slen][slen]; } int LongestLen() { int i, j, len; for (i = 0; i < slen; i++) maxlen[i][i] = 1; for (len = 1; len < slen; len++) { for (i = 0; i < slen - len; i++) { j = i + len; if (str[i] == str[j]) { if (i + 1 > j - 1) maxlen[i][j] = 2; else maxlen[i][j] = 2 + maxlen[i + 1][j - 1]; } else { maxlen[i][j] = Math.max(maxlen[i + 1][j], maxlen[i][j - 1]); } //print(); } } return maxlen[0][slen - 1]; } public void print() { for(int i=0;i<slen;++i) { for(int j=0;j<slen;++j){ System.out.print(""+maxlen[i][j]+" "); }System.out.println(); } System.out.println(); } public static void main(String arg[]) { Scanner s = new Scanner(System.in); String string = s.nextLine(); int l = new M2(string).LongestLen(); System.out.println(l); } }
0xFF总结
其实问题不难,就是在线笔试不能用IDE,效率骤降,调试也较难。References & Thx:http://blog.csdn.net/eclipse88/article/details/6473804(里面的代码有点问题,不过整体思路是一样的,十分感谢)