Created
October 16, 2012 11:07
-
-
Save zyxar/3898683 to your computer and use it in GitHub Desktop.
#amazon hiring 2012-2013 final 6 test 2 & solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*consider a kind of configuration file in amazon sofeware system. this kind of configuration file's format looks like this: | |
B=10; | |
A={ | |
A=100; | |
B=BDE; | |
C=C; | |
D={ | |
A=windows; | |
B=mac; | |
C={ | |
A=redhat; | |
B=ubuntu; | |
}; | |
}; | |
A+={ | |
A=200; | |
E=1000; | |
}; | |
to repsent the key of the configuration, we use period(.) delimated method. for example,A.B represents the element B in Map A, and the value of A.B is BDE; similarly, the value of A.D.C.A is redhat. the the represent string is called 'path expression'. | |
the configuration also support append and override operation. for the above example, we use += operation to append or override the value in Map A. now the value of A.A is 200, and the value of A.E is 1000; | |
Now, given a configuration strings and the key path of configuration, please return the value of configuration based the configuration strings. | |
Rules | |
1) the key name and his value only contains alphabet(A-Z,a-z) and number(0-9), no other charcters; | |
2) if cannot find the value or the expression point to a map, please output "N/A" | |
3) if find the value, please output the value. no spaces when output the value. | |
Input and Output | |
there are three part sin the input. the first line contains two integers indicates the number of congiruation lines(M) and the number of expressions(N). | |
M<=100, N<=100. the following M lines are the confugration and the last N lines are expression. every configuration line contains one or more configurations. every line length less than 1000. | |
Input : | |
2 2 | |
A={A=1;B=2;C=3;E={A=100;};}; | |
A+={D=4;E={B=10;C=D;};}; | |
A.E.B | |
B.D.E | |
Output | |
A.E.B=10 | |
B.D.E=N/A | |
*/ | |
#include <iostream> | |
using namespace std; | |
void parseArray(map<string, string>& result, string prefix, char* conf, int len); | |
void parseSingle(map<string, string>& result, string prefix, char* conf, int len) { | |
// A={}; A+={}; or A=1; | |
string line(conf, 0, len); | |
// cout<<"[sin] "<<line<<endl; | |
int a = line.find("+=", 0); | |
if (a == string::npos) { | |
a = line.find("=", 0); | |
} | |
string name = line.substr(0, a); | |
a = line.find("=", a) + 1; | |
if (conf[a] == '{') { | |
parseArray(result, prefix+"."+name, &conf[a+1], len-a-3); | |
} else { | |
int k = a; | |
while(conf[k] != ';') k++; | |
string value(conf, a, k-2); | |
// cout<<prefix<<"."<<name<<"="<<value<<endl; | |
result[prefix + "." + name] = value; | |
} | |
} | |
void parseArray(map<string, string>& result, string prefix, char* conf, int len) { | |
// A=1;B=2;C={...}; | |
// cout<<"[arr] "<<string(conf, 0, len)<<endl; | |
if (prefix[0] == '.') { | |
prefix = prefix.substr(1, prefix.length()); | |
} | |
int j = 0; | |
for (int i = 0; i < len; i++) { | |
if (conf[i] == '{') { | |
int c = 1; | |
while(c > 0) { | |
i++; | |
if (conf[i] == '{') c++; | |
else if(conf[i] == '}') c--; | |
} | |
parseSingle(result, prefix, &conf[j], i-j+2); | |
j = i+2; | |
} else if (conf[i] == ';') { | |
int ll = i-j+1; | |
if (ll > 0) parseSingle(result, prefix, &conf[j], ll); | |
j = i+1; | |
} | |
} | |
} | |
void calculateAndPrint(char* confs[], int confLength, char* exs[] , int exLength){ | |
//Your Code is here | |
map<string, string> result; | |
for (int i = 0; i < confLength; i++) | |
parseSingle(result, "", confs[i], strlen(confs[i])); | |
// map<string, string>::iterator it; | |
// for (it = result.begin(); it != result.end(); it++) { | |
// cout<<it->first<<"="<<it->second<<endl; | |
// } | |
for (int i = 0; i < exLength; i++) { | |
string pattern(exs[i]); | |
string& r = result[pattern]; | |
if (r == "") r = "N/A"; | |
cout<<pattern<<"="<<r<<endl; | |
} | |
} | |
int main(){ | |
int confLength=0; | |
int exLength=0; | |
cin>>confLength>>exLength; | |
cin.get(); | |
char* confs[10]; | |
char* exs[10]; | |
int i=0; | |
for(i=0;i<confLength;i++){ | |
char* data = new char[1000]; | |
gets(data); | |
confs[i] = data; | |
} | |
for(i=0;i<exLength;i++){ | |
char* data = new char[1000]; | |
gets(data); | |
exs[i] = data; | |
} | |
calculateAndPrint(confs,confLength,exs,exLength); | |
for(i=0;i<confLength;i++){ | |
delete confs[i]; | |
} | |
for(i=0;i<exLength;i++){ | |
delete exs[i]; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment