由于最近快要考ccsp了
前几次都在大模拟上吃了亏
最近练习用java和python写一些
201912-3 化学方程式
class Element: def __init__(self, CH): self.ch = CH def __str__(self): return self.ch def __lt__(self, other): if len(self.ch) == len(other.ch): return self.ch < other.ch else: return len(self.ch) > len(other.ch) # 获取s从p开始的数字 def getNum(s, p): coor = 0 pos = p for i in range(p, len(s)): if s[i].isdigit(): coor = coor * 10 + int(s[i]) else: pos = i break return coor, pos # 获取元素字典 def get(s): for i in range(10): st = str(i) s = s.replace(st, "") s = s.replace("(", "") s = s.replace(")", "") s = s.replace("=", "") s = s.replace("+", "") s += "#" res = [] size = len(s) for i in range(size - 1): if s[i].islower(): continue if s[i + 1].islower(): res.append(s[i:i + 2]) else: res.append(s[i:i + 1]) res = set(res) chars = [] for it in res: Ele = Element(it) chars.append(Ele) chars = sorted(chars) return chars # 处理一个括号的部分 def dfs(s, chars, P, mp): # print("now solving " + s) # print("P is ") # print(P) size = len(chars) res = [0 for _ in range(size)] pos = 0 N = len(s) s = s + "#" while (pos < N): if s[pos].isdigit(): pos += 1 continue elif s[pos].islower(): pos += 1 continue elif s[pos] == ')': pos += 1 continue elif s[pos] == '(': start = pos end = pos + P[pos] coor = 1 Pos = end + 1 if start + 1 < end: tmp = dfs(s[start + 1:end], chars, P[start + 1: end], mp) if s[end + 1].isdigit(): coor, Pos = getNum(s, end + 1) for i in range(size): res[i] += tmp[i] * coor pos = Pos else: # assert (s[pos] >= "A" and s[pos] <= "Z") ST = s[pos] if s[pos + 1].islower(): ST += s[pos + 1] pos += 1 cor = 1 p = pos + 1 if s[pos + 1].isdigit(): cor, p = getNum(s, pos + 1) res[mp[ST]] += cor pos = p return res # 处理某个化学式 def cal(s, chars, mp): # print("solve " + s) size = len(chars) # print("solving : " + s) # for i in range(size): # assert (s.count(chr(i + 65)) <= 1) coor = 1 pos = 0 if s[0].isdigit(): coor, pos = getNum(s, 0) s = s[pos:] # print("now s : " + s) # print("coor : " + str(coor)) sLen = len(s) P = [0 for _ in range(sLen)] for i in range(sLen): if s[i] != '(': continue cnt = 1 for j in range(i + 1, sLen): if s[j] == '(': cnt += 1 elif s[j] == ')': cnt -= 1 if cnt == 0: P[i] = j - i break # print("s is : " + s) # print("( position is : ") # print(P) # res = [0 for _ in range(size)] res = dfs(s, chars, P, mp) for i in range(size): res[i] = res[i] * coor # print("ans is ") # print(res) return res # 处理=左右的式子 def sol(s, chars, mp): size = len(chars) res = [0 for _ in range(size)] ls = s.split("+") for it in ls: cnow = cal(it, chars, mp) for i in range(size): res[i] += cnow[i] return res # 处理整个方程式 def solve(s): chars = get(s) # for it in chars: # print(it.ch, end=' ') # print() size = len(chars) mp = {} for i in range(size): mp[chars[i].ch] = i # print("before replace : " + s) # rep = [chr(i + 65) for i in range(size)] # for i in range(size): # s = s.replace(chars[i].ch, rep[i]) # print("after replace : " + s) ls = s.split("=") # assert (len(ls) == 2) c1 = sol(ls[0], chars, mp) c2 = sol(ls[1], chars, mp) ok = 1 for i in range(len(chars)): if c1[i] != c2[i]: ok = 0 break if ok == 1: print("Y") else: print("N") if __name__ == '__main__': n = int(input()) for i in range(n): s = input() solve(s) ''' 6 H2+O2=H2O 2H2+O2=2H2O H2+Cl2=2NaCl H2+Cl2=2HCl CH4+2O2=CO2+2H2O Cu+As=Cs+Au 4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH 1 4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH 1 CaCl2+2AgNO3=Ca(NO3)2+2AgCl 1 3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2 1 CH4+2O2=CO2+2H2O '''
这题没啥好说的,就写就完事了
老摸鱼大师了
201709-3 JSON查询
# -1 is : -2 is , -3 is 1" -4 is 2" def Dfs(s, dict, pos, pre): # print("now solving : " + s) size = len(s) p = 0 isKey = 1 Key = "" Value = "" now = "" while p < size: if pos[p] > 0: start = p + 1 end = p + pos[p] if start < end: Dfs(s[start:end], dict, pos[start:end], pre + Key + ".") Value = "@#$%^&**&^%$#@" dict[pre + Key] = Value now = "" Key = "" Value = "" isKey ^= 1 p = end + 1 else: if pos[p] == -3: now = "" elif pos[p] == -4: if isKey == 1: Key = now now = "" else: Value = now Value = Value.replace("\\", "\") Value = Value.replace("\"", """) dict[pre + Key] = Value now = "" Key = "" Value = "" isKey ^= 1 elif pos[p] != -1 and pos[p] != -2: now += s[p] p += 1 def solve(): n, m = map(int, input().split()) s = "" for i in range(n): ss = input() s += ss s = s.replace(" ", "") s = s.replace(" ", "") # print(s) size = len(s) # print(size) dict = {} pos = [0 for _ in range(size)] poos = [0 for _ in range(size)] ok = 1 p = 0 while p < size: if s[p] == '\': poos[p] = -1 p += 1 p += 1 for i in range(size): if s[i] == """ and poos[i - 1] != -1: if ok == 1: pos[i] = -3 else: pos[i] = -4 ok ^= 1 if ok == 0: continue if s[i] == "{": cnt = 1 can = 1 for j in range(i + 1, size): if s[j] == """ and s[j - 1] != "\": can ^= 1 if can == 0: continue if s[j] == "}": cnt -= 1 if cnt == 0: pos[i] = j - i break elif s[j] == "{": cnt += 1 elif s[i] == ':': pos[i] = -1 elif s[i] == ",": pos[i] = -2 # print(pos) Dfs(s[1:-1], dict, pos[1:-1], "") # print(dict) for i in range(m): q = input() q = q.replace("\", "\\") q = q.replace(""", "\"") if q in dict.keys(): if dict[q] == "@#$%^&**&^%$#@": print("OBJECT") else: print("STRING " + dict[q]) else: print("NOTEXIST") if __name__ == '__main__': solve() ''' 6 5 { "firs\"tName": "John", "lastName": "Smith", "address": "", "esc\aped": ""hello"" } firs"tName address address.city address.postal escaped 6 5 { "firstName": "John", "lastName": "Smith", "address": "", "escaped": "hello" } firstName address address.city address.postal escaped 13 0 { "A":"a", "B":{ "B":"b", "BB":"bb" }, "C":{ "D":{ "AA":"123", "qwe":"22" } } } 15 0 { "A":"a{", "B":{ "B}":"b", "BB":"bb" }, "C":{ "D":{ "A{}{{A":"123", "q:w:e":"22}}" } }, "TTT":"ttt", "GGG":"ggg" } 1 1 {"dsd\"":""\"} dsd" '''