ACM練習 題目1029:魔咒詞典 之最佳化

來源:互聯網
上載者:User

題目和解答在這裡:

ACM練習 題目1029:魔咒詞典 C++ map的使用

上次用C++的map做的,已耗用時間並不理想。

用了兩個map確實有點浪費,還有一點是把所有字串都在一個map中。

資料過多時尋找肯定慢。可以把map分成26個,以首字母分類,放在不同的map中。

#include <stdio.h>#include <string.h>#include <iostream>#include <map>using namespace std;map<string, string> map1[26]; //儲存<魔咒,對應功能>map<string, string> map2[26]; //儲存<對應功能,魔咒>int main() {char first;scanf("%c", &first);while (first == '[') {char temp[100]; //用臨時變數儲存剩下的整行gets(temp);char * text = strchr(temp, ']'); //找到 ] 的位置int len = strlen(temp) - strlen(text);text += 2; //得到後半部分的 char *string value(text);temp[len] = '\0';string key(temp);map1[key[0] - 'a'].insert(pair<string, string>(key, value));map2[value[0] - 'a'].insert(pair<string, string>(value, key));scanf("%c", &first);}char aaaa[10];gets(aaaa); //把剩餘的讀取了int n;scanf("%d\n", &n);map<string, string>::iterator it;for (int i = 0; i < n; i++) {string str;getline(cin, str);if (str.at(0) == '[') {str = str.substr(1, str.length() - 2);it = map1[str[0] - 'a'].find(str);if (it != map1[str[0] - 'a'].end())cout << it->second << endl;elseprintf("what?\n");} else {it = map2[str[0] - 'a'].find(str);if (it != map2[str[0] - 'a'].end())cout << it->second << endl;elseprintf("what?\n");}}return 0;}

時間為60ms,還是不夠好。

其實可以不用map,使用vector

#include <cstdio>#include <cstring>#include <string>#include <vector>using namespace std;typedef struct {string name, func;} dict_def;vector<int> dict_indexed_by_name[26], dict_indexed_by_func[26];vector<int>::iterator it_vec;vector<dict_def> dict;int  item_num;void Parse(char p_input[]) {char s_name[20], s_func[80], *pos;pos = strchr(p_input,']');strcpy(s_func, pos + 2);*pos = '\0';strcpy(s_name, p_input + 1);string s1(s_name), s2(s_func);dict_def a_word;a_word.name = s1, a_word.func = s2;dict.push_back(a_word);dict_indexed_by_name[s_name[0] - 'a'].push_back(item_num);/*該魔法單詞的序號*/dict_indexed_by_func[s_func[0] - 'a'].push_back(item_num);item_num++;}string FindName(string p_func)/*按功能找魔法名*/{int index = p_func[0] -'a';for (it_vec = dict_indexed_by_func[index].begin();it_vec < dict_indexed_by_func[index].end(); it_vec++)if (dict[(*it_vec)].func == p_func)break;if (it_vec < dict_indexed_by_func[index].end())return dict[(*it_vec)].name;return "";}string FindFunc(string p_name)/*按魔法名找功能*/{int index = p_name[0] - 'a';for (it_vec = dict_indexed_by_name[index].begin();it_vec < dict_indexed_by_name[index].end(); it_vec++)if (dict[(*it_vec)].name == p_name)break;if (it_vec < dict_indexed_by_name[index].end())return dict[(*it_vec)].func;return "";}int main() {char input[100];while (gets(input) != NULL) {while (strcmp(input, "") == 0)gets(input);if (strcmp(input, "@END@") == 0)break;Parse(input);/*將一條記錄拆分成單詞名稱和功能,並存入相應vector*/}int case_num;string s_name, s_func, ans;scanf("%d", &case_num);getchar();while (case_num--) {gets(input);if (input[0] == '[') {s_name = input;s_name.erase(s_name.begin());s_name.erase(s_name.end() - 1);ans = FindFunc(s_name);printf("%s\n", ans == "" ? "what?" : ans.c_str());} else {s_func = input;ans = FindName(s_func);/*真正進行對比的數組,數組下標,要對比的字串*/printf("%s\n", ans == "" ? "what?" : ans.c_str());}}return 0;}

時間為10ms

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.