代码实现能力啊!!!!

描述

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.

样例输入
2
A<B<=E
3<=E<5
2
A 1
B 2
E 3
A 3
B 5
E 10
样例输出
Yes
No 
package l176;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Map<String, String> m1 = new HashMap<String, String>(), m2 = new HashMap<String, String>();
		Set<String> set = new HashSet<String>();

		for(int i=0; i<n; i++) {
			String s = sc.next();
			char[] cs = s.toCharArray();
			List<Integer> l = new ArrayList<Integer>();
			for(int j=0; j<cs.length; j++) {
				if(cs[j] == '<') l.add(j);
			}
			
			List<String> l2 = new ArrayList<String>();
			for(int j=0; j<l.size(); j++) {
				if(j == 0)	l2.add(s.substring(0, l.get(0)));
				else {
//					int start = cs[l.get(j-1)+1]=='=' ?s.substring(l.get(j-1)+2, l.get(j));
					String v = cs[l.get(j-1)+1]=='=' ?s.substring(l.get(j-1)+2, l.get(j)):s.substring(l.get(j-1)+1, l.get(j));
					l2.add(v);
				}
			}
			l2.add(s.substring(1+Math.max(s.lastIndexOf('='), s.lastIndexOf('<'))));
			
			for(String t : l2) {
				if(t.charAt(0)>='A'&&t.charAt(0)<='Z')	set.add(t);
			}
			
			for(int t=0; t<l.size(); t++) {
				if(s.charAt(l.get(t)+1) == '=')
					m2.put(l2.get(t), l2.get(t+1));
				else
					m1.put(l2.get(t), l2.get(t+1));
			}
			
		}
		
		int m = sc.nextInt(), k = set.size();
//		System.out.println(m+" " +k);
		for(int i=0; i<m; i++) {
			boolean f = true;
			Map<String, Integer> m3 = new HashMap<String, Integer>();
			for(int j=0; j<k; j++) {
				m3.put(sc.next(), Integer.valueOf(sc.next()));
			}
			
			for(String key : m1.keySet()) {
				int a = -1, b = -1;
				try {
					a = Integer.valueOf(key);
				} catch(Exception e) {
					a = Integer.valueOf(m3.get(key));
				}
				try {
					b = Integer.valueOf(m1.get(key));
				} catch(Exception e) {
					b = Integer.valueOf(m3.get(m1.get(key)));
				}
				if(a >= b) {
					f = false;
					break;
				}
			}
			
			for(String key : m2.keySet()) {
				int a = -1, b = -1;
				try {
					a = Integer.valueOf(key);
				} catch(Exception e) {
					a = Integer.valueOf(m3.get(key));
				}
				try {
					b = Integer.valueOf(m2.get(key));
				} catch(Exception e) {
					b = Integer.valueOf(m3.get(m2.get(key)));
				}
				if(a > b) {
					f = false;
					break;
				}
			}
			
			if (f)
				System.out.println("Yes");
			else
				System.out.println("No");
		}
	}
}



Given a chemical formula (given as a string), return the count of each atom.

An atomic element always starts with an uppercase character, then zero or more lowercase letters, representing the name.

1 or more digits representing the count of that element may follow if the count is greater than 1. If the count is 1, no digits will follow. For example, H2O and H2O2 are possible, but H1O2 is impossible.

Two formulas concatenated together produce another formula. For example, H2O2He3Mg4 is also a formula.

A formula placed in parentheses, and a count (optionally added) is also a formula. For example, (H2O2) and (H2O2)3 are formulas.

Given a formula, output the count of all elements as a string in the following form: the first name (in sorted order), followed by its count (if that count is more than 1), followed by the second name (in sorted order), followed by its count (if that count is more than 1), and so on.

Example 1:

Input: 
formula = "H2O"
Output: "H2O"
Explanation: 
The count of elements are {'H': 2, 'O': 1}.

Example 2:

Input: 
formula = "Mg(OH)2"
Output: "H2MgO2"
Explanation: 
The count of elements are {'H': 2, 'Mg': 1, 'O': 2}.

Example 3:

Input: 
formula = "K4(ON(SO3)2)2"
Output: "K4N2O14S4"
Explanation: 
The count of elements are {'K': 4, 'N': 2, 'O': 14, 'S': 4}.

Note:

  • All atom names consist of lowercase letters, except for the first character which is uppercase.
  • The length of formula will be in the range [1, 1000].
  • formula will only consist of letters, digits, and round parentheses, and is a valid formula as defined in the problem.

    package l726;
    
    import java.util.TreeMap;
    
    /*
     * 思路是知道的
     * 代码实现不行
     */
    class Solution {
        public String countOfAtoms(String formula) {
        	TreeMap<String, Integer> m = countOfAtoms2(formula);
            StringBuilder s = new StringBuilder();
            for(String k : m.keySet())
            	s.append(k).append(m.get(k)==1?"":m.get(k));
            return s.toString();
        }
    
    	private TreeMap<String, Integer> countOfAtoms2(String formula) {
    		TreeMap<String, Integer> m = new TreeMap<String, Integer>();
            int i = formula.indexOf('(');
            
            TreeMap<String, Integer> m1 = getNoBracket(formula, 0, i==-1?formula.length()-1:i-1);
        	for(String k : m1.keySet()) 
        		m.put(k, m.containsKey(k)?m.get(k)+m1.get(k):m1.get(k));
        	
        	// 后面可能有多个连续的括号。。。。
            if(i != -1) {
            	int j = i+1, cnt = 1;
            	while(cnt != 0) {
            		if(formula.charAt(j) == '(')	cnt++;
            		else if(formula.charAt(j)==')')	cnt--;
            		j++;
            	}
            	int k = j;
            	while(k<formula.length() && formula.charAt(k)>='0' && formula.charAt(k)<='9')	k++;
            	int r = Integer.valueOf(formula.substring(j,k));
            	TreeMap<String, Integer> m2 = countOfAtoms2(formula.substring(i+1, j-1));
            	for(String tt : m2.keySet()) 
            		m.put(tt, m.containsKey(tt)?m.get(tt)+r*m2.get(tt):r*m2.get(tt));
            	
            	TreeMap<String, Integer> m3 = countOfAtoms2(formula.substring(k));
            	for(String tt : m3.keySet()) 
            		m.put(tt, m.containsKey(tt)?m.get(tt)+m3.get(tt):m3.get(tt));
            	
            }
            return m;
    	}
    
    	private TreeMap<String, Integer> getNoBracket(String formula, int s, int t) {
    		TreeMap<String, Integer> m = new TreeMap<String, Integer>();
    		int i = s, j = s;
    		char[] cs = formula.toCharArray();
    		while(j <= t) {
    			while(j<=t && ((i==j) || (cs[j]>='a'&&cs[j]<='z'))) j++;
    			String atom = formula.substring(i,j);
    			i = j;
    			if(j>t || !(cs[j]>='0'&&cs[j]<='9')) {
    				m.put(atom, m.containsKey(atom)?1+m.get(atom):1);
    			} else {
    				while(j<=t && (cs[j]>='0'&&cs[j]<='9')) j++;
    				int cnt = Integer.valueOf(formula.substring(i,j));
    				m.put(atom, m.containsKey(atom)?cnt+m.get(atom):cnt);
    			}
    			i = j;
    		}
    		return m;
    	}
    }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值