去哪儿笔试
发布于 2017-10-11 10:05 3295 次浏览 0 赞 来自 笔试面试  
1同学们,我们刚刚经历了十一和中秋双节假期,让我们一起预祝我们的祖国繁荣富强,同学们每个中秋与家人共赏明月,也期待同学们能够加入去哪儿网,为大家的出游和回家团圆尽一份力量。
我们的第二题是这样的,给出一个以空格作为分隔符的字符串,求其与其空格分隔的逆序字符串的最长公共子序列长度。
输入
例如:输入 2017 11 02
其逆序字符串为 02 11 2017
输出
6(2 11 2)

样例输入
2017 11 02
样例输出
6


一张机票的价格是由多个因素决定的,它通常与飞行距离没有直接的关系。许多旅行者于是在这方面变得非常有创意,当飞机在多个城市停靠时,只是用机票的一部分,以实现低花费的旅行。例如:北京到温哥华的机票可能卖8000元人民币,但是,北京-温哥华-西雅图的机票可能卖7500元,如果用户的目的地是温哥华,那么用户会选择买北京-温哥华-西雅图的机票,当他乘坐完北京-温哥华的航段后,会放弃乘坐温哥华到西雅图的航段。然而,航空公司也了解这种行为,并通常要求一张机票所包含的站点必须要按顺序旅行,而且不允许中途加入其他路线。例如:你手中有一张从北京到温哥华然后再到西雅图的机票,你不能仅使用机票中温哥华到西雅图的部分,你必须从机票上的第一个城市北京出发;此外,也不允许你从城市北京到城市温哥华,然后去一些其他地方如多伦多并返回温哥华,再继续你从温哥华到西雅图的旅途。
给出一组优惠的机票,以及一条或多条旅游路线,你要确定为了使旅行费用最少,应该如何购买机票。
现假设:优惠机票航线不多于10条,每组优惠机票的测试用例旅行路线不多于10条,每张机票的航段数不多于5个,每个优惠航线票价不高于10000元
输入
包含一组测试用例,测试用例中描述一组优惠机票和一组旅行路线
每组测试用例由4部分组成:
第1行为优惠机票航线有多少条(n)
第2行~第2+n-1行描述优惠机票编号,每张优惠机票的价格、航段数和航段顺序
第2+n行描述了测试用例的旅行路线
输出
对于每条旅行路线,输出两行,包括测试用例编号、路线编号、路线的最小花费;然后按使用顺序输出本次旅行所使用的机票编号,具体输出格式见样例,保证答案唯一。如果输入参数不合法,则返回Error。

样例输入
3
1 700 2 HongKong Seattle
2 700 3 Beijing Seattle Vancouver
3 1400 3 Beijing HongKong Vancouver
3 Beijing HongKong Seattle
样例输出
2100
3 1


5 条回复
acmcoderboAIcBEU 回复 acmcoderwreJoRbk

求第一题代码

2017-10-11 10:17

import java.util.Scanner;



public class Main77 {

static int[][] c;

static int[][] b;

public static void main(String[] args) {

// TODO Auto-generated method stub

Scanner scanner = new Scanner(System.in);

String x = scanner.nextLine();

char[] temp = x.toCharArray();

String[] y = x.split(" ");

String t = "";

for (int i = 0; i < y.length; i++) {

t = t+y[y.length-1-i]+" ";

}

c=new int[1000][1000];

        b=new int[1000][1000];

        int m=x.length();

        int n=x.length();

        //System.out.println(t);

        LCSLength(m,n,temp,t.toCharArray(),c,b);

        System.out.println(c[m][n]);

        //LCS(temp.length,t.length(),temp,b);

        scanner.close();

}

    static void LCSLength(int m,int n,char[] x,char[] y,int[][] c,int[][] b){

    int i,j;

    for(i=1;i<=m;i++) c[i][0]=0;

    for(i=1;i<=n;i++) c[0][i]=0;

    for(i=1;i<=m;i++){

    for(j=1;j<=n;j++){

    //System.out.println(x[i-1]+" "+y[j-1]+(x[i-1]==y[j-1]));

    if(x[i-1]==y[j-1]){

    c[i][j]=c[i-1][j-1]+1;

    b[i][j]=1;

    }else if(c[i-1][j]>=c[i][j-1]){

    c[i][j]=c[i-1][j];

    b[i][j]=2;

    }else{

    c[i][j]=c[i][j-1];

    b[i][j]=3;

    }

    }

    }

}

    static void LCS(int i,int j,char[] x,int[][] b){

    if(i==0 || j==0) return;

    if(b[i][j]==1){

    LCS(i-1,j-1,x,b);

    System.out.print(x[i]);

    }else if(b[i][j]==2){

    LCS(i-1,j,x,b);

    }else{

    LCS(i,j-1,x,b);

    }

    }

}


方法虽笨,但至少能够AC

2017-10-11 10:17

public class Main {


public static void main(String[] args) {

Scanner reader = new Scanner(System.in);

String sourceStr = reader.nextLine();

if(sourceStr == null || sourceStr.length() == 0){

return;

}

if(sourceStr == " " || sourceStr.charAt(sourceStr.length()-1)+"" == " "){

return;

}

String reverseStr = sentenceReverse(sourceStr);

// String[] sourceSplit = sourceStr.split(" ");

// String[] reverseSplit = reverse.split(" ");  //长度相等

//最长子序列- 动态规划- 每个子串求最长公共子序列

int res = 0;

res = findLCS(sourceStr, reverseStr);

System.out.println(res);

}


public static int findLCS(String A, String B) {

int n = A.length();

int m = B.length();

char[] a = A.toCharArray();

char[] b = B.toCharArray();

int[][] dp = new int[n][m];

for (int i = 0; i < n; i++) {// 第一列

if (a[i] == b[0]) {

dp[i][0] = 1;

for (int j = i + 1; j < n; j++) {

dp[j][0] = 1;

}

break;

}

}


for (int i = 0; i < m; i++) {// 第一行

if (a[0] == b[i]) {

dp[0][i] = 1;

for (int j = i + 1; j < m; j++) {

dp[0][j] = 1;

}

break;

}

}

for (int i = 1; i < n; i++) {

for (int j = 1; j < m; j++) {

if (a[i] == b[j]) {

dp[i][j] = dp[i - 1][j - 1] + 1;

} else {

dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);

}

}

}

for (int i = 0; i < n; i++) {

for (int j = 0; j < m; j++) {

}

}

return dp[n - 1][m - 1];

}

//反转字符串

public static String reverse(String source) {


char[] array = source.toCharArray();


int n = source.length() - 1;


int halfLength = n / 2;


for (int i = 0; i <= halfLength; i++) {


char temp = array[i];

array[i] = array[n - i];

array[n - i] = temp;

}

return new String(array);

}


//句子反转

public static String sentenceReverse(String snetence) {


StringBuilder builder = new StringBuilder();


String reverseSentence = reverse(snetence);


String[] split = reverseSentence.split(" ");


for (int i = 0; i < split.length; i++) {


builder.append(reverse(split[i]) + " ");

}


return builder.toString();

}

}


2017-10-11 11:06

话说考试中途还可以这么查的吗??????   这也太水了吧?????

2017-10-11 11:07

连题目都没有看懂

2017-10-11 11:30
添加回复
回到顶部