1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| #include<iostream> #include<vector> #include<string> #include<stdio.h> #include<algorithm> #include<cstring>
using namespace std;
typedef struct{ string s; int num; }mystring; void out(mystring x){ cout<<x.s<<" "<<x.num<<endl; } void outString(vector<mystring> e){ for_each(e.begin(),e.end(),out); cout<<endl; } bool isnumber(char c){ if(c>='0'&&c<='9') return true; return false; } bool isbig(char c){ if(c>='A'&&c<='Z') return true; return false; } bool issmall(char c){ if(c>='a'&&c<='z') return true; return false; } int find(vector<mystring> m,string x){ int len = m.size(); for(int i=0;i<len;i++){ if(m[i].s==x) return i; } return -1; } bool cmp(mystring x1,mystring x2){ if(x1.s[0]-x2.s[0]>0) return true; else if(x1.s[0]-x2.s[0]==0){ int size1 = x1.s.size(); int size2 = x2.s.size(); if(size1>size2){ return true; } if(x1.s[1]-x2.s[1]>0){ return true; } } return false; } int tonumber(string s,int i){ int len = s.length(); int ans = 0; while(i<len&&isnumber(s[i])){ ans = ans*10 + s[i] - '0'; i++; } if(ans==0) ans = 1; return ans; } void func(string s,vector<mystring> &ans,int time){ int len = s.length(); if(isnumber(s[0])){ time = tonumber(s,0); } for(int i=0;i<len;i++){ if(isbig(s[i])){ mystring t; if(i+1<len){ if(issmall(s[i+1])){ t.s.assign(s,i,2); int num = tonumber(s,i+2); t.num = time*num; } else if(isnumber(s[i+1])){ t.s.assign(s,i,1); int num = tonumber(s,i+1); t.num = time*num; } else{ t.s.assign(s,i,1); t.num = time; } } else{ t.s.assign(s,i,1); t.num = time; } int index = find(ans,t.s); if(index==-1) ans.push_back(t); else{ ans[index].num += t.num; } } else if(s[i]=='('){ int num_k = 1; for(int j=i+1;j<len;j++){ if(s[j]=='(') num_k++; else if(s[j]==')'){ num_k--; if(num_k==0){ string sub; sub.assign(s,i+1,j-i-1); int num = tonumber(s,j+1); int time_copy = num * time; func(sub,ans,time_copy); i = j; } } } } } } void process_expression(vector<string> e,vector<mystring> &ans){ int len = e.size(); for(int i=0;i<len;i++){ func(e[i],ans,1); } } void process(){ char tmp[1000]; vector<string> expression; gets(tmp); char *sp = strtok(tmp,"="); while(sp){ expression.push_back(sp); sp = strtok(NULL,"="); } vector<string> expression1; vector<string> expression2; char * tmp1 = (char*)expression[0].c_str(); char *sp1 = strtok(tmp1,"+"); while(sp1){ expression1.push_back(sp1); sp1 = strtok(NULL,"+"); } char * tmp2 = (char*)expression[1].c_str(); char *sp2 = strtok(tmp2,"+"); while(sp2){ expression2.push_back(sp2); sp2 = strtok(NULL,"+"); } vector<mystring> ans1; process_expression(expression1,ans1); vector<mystring> ans2; process_expression(expression2,ans2); sort(ans1.begin(),ans1.end(),cmp); sort(ans2.begin(),ans2.end(),cmp); int len1 = ans1.size(); int len2 = ans2.size(); if(len1!=len2){ cout<<'N'<<endl; return; } for(int i=0;i<len1;i++){ if(ans1[i].num!=ans2[i].num||ans1[i].s.compare(ans2[i].s)!=0){ cout<<'N'<<endl; return; } } cout<<'Y'<<endl; return; }
int main(){ int n; cin>>n; char tmp = getchar(); for(int i=0;i<n;i++){ process(); } return 0; }
|