活跃农民
- 积分
- 380
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2014-7-20
- 最后登录
- 1970-1-1
|
|
class SolutionIBM1 { public: string replaceNumber(int leng, int a, int b) { vector<string> result; string output=""; for(int i=0; i<leng; i++) { string curr=""; if( (isDivisible(i+1, a) || isDivisible(i+1, b)) || (hasDigit(i+1, a) || hasDigit(i+1, b))){ if(isDivisible(i+1, a) || isDivisible(i+1, b)) curr+="OUT"; if(hasDigit(i+1, a) || hasDigit(i+1, b)) { curr+="THINK"; } } else { curr+=to_string(i+1); } result.push_back(curr); } for(int i=0; i<result.size(); i++) { if(i==0) { output+=result[i]; } else { output+="," + result[i]; } } return output; } void test() { cout<<replaceNumber(20, 3, 4)<<endl; cout<<replaceNumber(7, 2, 3)<<endl; } private: bool isDivisible(int i, int a) { if(a==0) { return false; } if(i%a==0) { return true; } return false; } bool hasDigit(int i, int a) { string ii = to_string(i); string aa = to_string(a); if(ii.find(aa)!=string::npos) { return true; } return false; } }; class SolutionIBM2 { //Trie public: class TrieNode{ public: bool isWord; TrieNode* next[26]; TrieNode(bool b=false) { memset(next, 0, sizeof(next)); isWord=b; } }; SolutionIBM2() { root=new TrieNode(); } void insert(string s) { TrieNode *p = root; for(int i=0; i<s.size(); i++) { if(p->next[s[i]-'A']==NULL) { p->next[s[i]-'A']=new TrieNode(); } p=p->next[s[i]-'A']; } p->isWord=true; } bool search(string key) { TrieNode *p = find(key); return p!=NULL && p->isWord; } vector<string> startsWith(string prefix) { TrieNode* p = find(prefix); vector<string> result; if(p==NULL) { result.push_back("<NONE>"); return result; } vector<int> path; dfsHelper(p, path, result); for(int i=0; i<result.size(); i++) { result[i]= prefix+result[i]; } return result; } void test() { insert("ACETAMINOPHEN"); insert("ASPERGEL"); insert("ASPRIN"); insert("ASPERTAME"); insert("ATAVAN"); insert("ATBCDEW"); insert("BUPROPION"); cout<<search("AT")<<endl; vector<string> result = startsWith("BD"); for (int i=0; i<result.size(); i++) { cout<<result[i]<<endl; } } private: void dfsHelper(TrieNode* root, vector<int>& path, vector<string>& result) { if(root==NULL) { return; } if(root->isWord==true) { string curr = ""; for(int i=0;i<path.size(); i++) { curr += 'A'+path[i]; } result.push_back(curr); } for(int j=0; j<26; j++) { if(root->next[j]!=NULL) { path.push_back(j); dfsHelper(root->next[j], path, result); path.pop_back(); } } } TrieNode* find(string key) { TrieNode *p = root; for(int i=0; i<key.size() && p!=NULL; i++) { p=p->next[key[i]-'A']; } return p; } TrieNode* root; }; class SolutionIBM3 { public: vector<string> parser(vector<string> input) { vector<string> result; unordered_map<string, unordered_map<string, int>> dict; for (int i=0; i<input.size(); i++) { vector<string> curr; string token; istringstream ss(input[i]); while(getline(ss, token, ',')) { curr.push_back(token); } if (curr.size()==3) { if (dict.find(curr[0])==dict.end()) { unordered_map<string, int> temp; temp[curr[2]]=stoi(curr[1]); dict[curr[0]]=temp; } else { dict[curr[0]][curr[2]]+=stoi(curr[1]); } } } for (auto it = dict.begin(); it != dict.end(); ++it ) { string curr = it->first; int all=0, unit=0; string avg; for(auto itt = it->second.begin(); itt!=it->second.end(); ++itt) { unit++; all+=itt->second; } avg=to_string((float)all/unit); curr+=","+to_string(all); curr+=","+avg.substr(0,avg.find('.')+3); curr+=","+to_string(unit); result.push_back(curr); } return result; } void test() { vector<string> input; input.push_back("2017-06-02,5,Apples"); input.push_back("2017-06-02,2,pears"); input.push_back("2017-06-03,3,pineapples"); vector<string> result = parser(input); for(int i=0; i<result.size(); i++) { cout<<result[i]<<endl; } } }; |
|