【hihocoder#1341】(微软探星夏令营在线技术笔试第一题)

来源:互联网 发布:驻马店公务员网络培训 编辑:程序博客网 时间:2024/05/17 08:23

第一次写这样的博客,毕竟很少在技术上追求一些东西,昨天微软探星夏令营的笔试被虐了,还是有不少感慨的,这里分享下第一题吧。

平时都是写工程里面简单的业务逻辑,很少做类似的oj题目,所以挺尴尬的只会一点点,代码很乱,我会适当解释一下哈。

本博客只是简单分享,顺便给自己9月份找工作攒人品,大神请无视我蹩脚的代码。

言归正传看题目:

#1341 : Constraint Checker

时间限制:10000ms
单点时限:1000ms
内存限制:256MB

描述

Given a set of constraints like 0<N<=M<=100 and values for all the variables, write a checker program to determine if the constraints are satisfied.

More precisely, the format of constraints is:

token op token op ... op token

where each token is either a constant integer or a variable represented by a capital letter and each op is either less-than ( < ) or less-than-or-equal-to ( <= ). 

输入

The first line contains an integer N, the number of constraints. (1 ≤ N ≤ 20)

Each of the following N lines contains a constraint in the previous mentioned format.

Then follows an integer T, the number of assignments to check. (1 ≤ T ≤ 50)

Each assignment occupies K lines where K is the number of variables in the constraints.

Each line contains a capital letter and an integer, representing a variable and its value.

It is guaranteed that:

1. Every token in the constraints is either an integer from 0 to 1000000 or an variable represented by a capital letter from 'A' to 'Z'.

2. There is no space in the constraints.

3. In each assignment every variable appears exactly once and its value is from 0 to 1000000. 

输出

For each assignment output Yes or No indicating if the constraints are satisfied.

样例输入
2A<B<=E3<=E<52A 1B 2E 3A 3B 5E 10
样例输出
YesNo 
题目翻译:

就是一个约束匹配,第一排输入规则条数N(0-20),接下来根据条数N输入规则。

然后就是输入测试数据,先输入测试数据组数,然后输入测试数据的个数(这里需要注意,要根据规则里面字母出现的种类多少来输入)

解法和难点(个人的,有更好的欢迎留言或者贴地址):

1.利用规则,规则就是简单的< <= ,不难想到其实这就是一个不减的排序数据,去掉=(replace函数)然后利用<(split函数)来分割字符串就可以得到一个不减的排序数组了。

2.统计好规则中出现的字母种类数据,然后利用输入的数据替换掉字母得到N条规则得到的N排不减的数据

3.写一个简答的判断函数来判断这N排数据是否不减。

我的解法就是这样,主要还是对于oj题目不熟悉,输入输出的控制花了很多时间。

JAVA版本代码如下,写的非常粗糙,很多for循环,时间复杂度有点高,但是成功AC了,大家可以适当看看自己改写下:

import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.Scanner;public class Main {    public void bubbleSort(int[] array) {        int temp;        for(int i=0;i<array.length;i++){          for(int j=0;j<array.length-i-1;j++){            if(array[j]>array[j+1]){              temp=array[j];              array[j]=array[j+1];              array[j+1]=temp;            }          }        }    }    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int n = scanner.nextInt();        String[] rule = new String[n];        String rules = "";        for (int i = 0; i < n; i++) {            String buffer = new String(scanner.next());    rule[i] = buffer;    rules = rules +buffer;        }        rules = rules.replace("<=","");        rules = rules.replace("<","");        for (int i = 0; i <= 9; i++) {            rules = rules.replace(Integer.toString(i),"");        }        List<String> data = new ArrayList<String>();                for (int i = 0; i < rules.length(); i++) {            String s = rules.substring(i, i + 1);            if (!data.contains(s)) {                data.add(s);            }        }        String result = "";        for (String s : data) {            result += s;        }        int m = scanner.nextInt();        for (int i = 0; i < m; i++) {        String[] zimu = new String[result.length()];        int[] shuzi = new int[result.length()];        boolean flag = true;        for (int j = 0; j < result.length(); j++) {            zimu[j] = scanner.next();            shuzi[j] = scanner.nextInt();        }        for(int l=0;l<n;l++){        String temp = rule[l];        temp = temp.replace("=","");        for(int k=0;k<result.length();k++){            temp = temp.replace(zimu[k],Integer.toString(shuzi[k]));        }        String[] strArr = temp.split("<");        int[] numberArr = new int[strArr.length];            for (int s = 0; s <strArr.length; s++)        {            numberArr[s] = Integer.parseInt(strArr[s]);        }            Main ceshi = new Main();            int[] newnumberArr = new int[strArr.length];            for (int s = 0; s <strArr.length; s++)        {            newnumberArr[s] = Integer.parseInt(strArr[s]);        }            ceshi.bubbleSort(newnumberArr);            if (!Arrays.equals(numberArr, newnumberArr)) {            flag = false;}        }        if(flag)        {        System.out.println("Yes");        }        else {        System.out.println("No");}        }        }}


1 0