九度:1029<魔咒词典><排序查找>
2014-03-06 08:44
274 查看
给出多个版本。前两个版本分别为C,C++。是论坛里面他人AC的。自己写的九度一直不AC,但杭电AC了。再想想哪里有边界吧!
C语言版
/* 748868 ****** 1029 Accepted 点击此处查看所有case的执行结果 20972KB 2240B 20MS C++ / 代码 / 编辑 22:02:09 748860 ****** 1029 Accepted 点击此处查看所有case的执行结果 2584KB 1847B 40MS C++ / 代码 / 编辑 22:00:54 第一条为本C语言的版本运行。第二条为C++版本。 C语言中内存耗的大,在于使用固定大小内存。而C++ 版本中使用了STL,内存自动增长。 */ #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; const int MAXLEN = 81; const int MAXN = 100001; char tem[MAXLEN]; struct diction{ char mz[21]; char fun[MAXLEN]; }dic[MAXN]; struct diction2{ char mz[21]; char fun[MAXLEN]; }dic2[MAXN]; int k; bool dic_cmp(struct diction x, struct diction y){return strcmp(x.mz, y.mz)<0;} bool dic_cmp2(struct diction2 x, struct diction2 y){return strcmp(x.fun, y.fun)<0;} void find(char *ans, char *tem) { int L=0, R=k; while(L<R) {//二分查找 int mid = L + (R-L)/2; if(strcmp(dic[mid].mz, tem)==0) { strcpy(ans, dic[mid].fun); return; } if(strcmp(dic[mid].mz, tem) > 0){R=mid;} else {L=mid+1;} } strcpy(ans, "what?"); } void find2(char *ans, char *tem) { int L=0, R=k; while(L<R) {//二分查找 int mid = L + (R-L)/2; if(strcmp(dic2[mid].fun, tem)==0) { strcpy(ans, dic2[mid].mz); return; } if(strcmp(dic2[mid].fun, tem) > 0){R=mid;} else {L=mid+1;} } strcpy(ans, "what?"); } int main() { //freopen("JDOJ.1029.IN", "r", stdin); int n; k=0; while(fgets(tem, MAXLEN, stdin), strstr(tem, "@END@")==NULL) { int len = strlen(tem); tem[len-1] = 0; char *s = strchr(tem, ']'); *s = 0; strcpy(dic[k].mz, tem+1); strcpy(dic[k].fun, s+2); strcpy(dic2[k].mz, dic[k].mz); strcpy(dic2[k++].fun, dic[k].fun); } sort(dic2, dic2+k, dic_cmp2); sort(dic, dic+k, dic_cmp);//排序 scanf("%d\n", &n); char ans[MAXLEN]; while(n--) { fgets(tem, MAXLEN, stdin); int len = strlen(tem); tem[--len] = 0; if(tem[0]=='[') {tem[--len] = 0;find(ans, tem+1);} else {find2(ans, tem);} printf("%s\n", ans); } //while(1); return 0; }
C++版
/* 命令不一定只有一个单词,只用一个单词的话,WA。 */ #include<iostream> #include<fstream> #include<algorithm> #include<string> #include<map> using namespace std; map<string,string> dic[2]; map<string,string>::iterator iter; int main() { int i,n; string str,word,meaning,strn; // fstream cin("1029.txt"); cin.ignore(); getline(cin,str); while (str!="@END@") { i=str.find("]"); word=str.substr(1,i-1); meaning=str.substr(i+2); dic[0].insert(pair<string,string>(word,meaning)); dic[1].insert(pair<string,string>(meaning,word)); getline(cin,str); } cin>>n; cin.ignore(); while (n--) { getline(cin,str); i=str.find("]"); if (i==-1) { iter=dic[1].find(str); if (iter!=dic[1].end()) { cout<<iter->second<<endl; } else { cout<<"what?"<<endl; } } else { str=str.substr(1,i-1); iter=dic[0].find(str); if (iter!=dic[0].end()) { cout<<iter->second<<endl; } else { cout<<"what?"<<endl; } } } return 0; }
HDOJ版
#include <stdio.h> #include <cstring> #include <stdlib.h> #include <iostream> #include <algorithm> #define SIZE 100005 using namespace std; struct Node{ char order[30]; char meaning[100]; }; //-------------------- Node dic[SIZE], dic2[SIZE]; int len; //-------------------- void Init() { memset(dic, 0, sizeof(dic)); memset(dic2, 0, sizeof(dic2)); len =0; } void Input() { char str[100]; memset(str, 0, sizeof(str)); while(gets(str) && strcmp(str, "@END@") != 0) { // printf("%s\n", str); int j=0, i =0; for(i=1; str[i] != ']'; i++) { dic[len].order[j] = dic2[len].order[j] =str[i]; j++; } dic[len].order[j] = dic2[len].order[j] ='\0'; // printf("order:%s ", dic[len].order); j=0; for(i=i+2; str[i] != '\n'; i++) { dic[len].meaning[j] = dic2[len].meaning[j] = str[i]; j++; } dic[len].meaning[j] = dic2[len].meaning[j] = '\0';//对字符串一个一个按字符处理,一定要添加\0呢 // printf("mean:%s\n", dic[len].meaning); len++;//读完一行 memset(str, 0, sizeof(str)); } } //按词排序 bool cmp1(Node a, Node b) { return strcmp(a.order, b.order) < 0; } bool cmp2(Node a, Node b) { return strcmp(a.meaning, b.meaning) < 0; } void Find1(char t[]) { int low = 0, high = len-1, mid; while(low <= high) { mid = (low + high)/2; if(strcmp(t, dic[mid].order) < 0) { high = mid-1; } else if(strcmp(t, dic[mid].order) > 0) { low = mid+1; } else { printf("%s\n", dic[mid].meaning); return ; } } printf("what?\n"); } void Find2(char t[]) { int low = 0, high = len-1, mid; while(low <= high) { mid = (low + high)/2; if(strcmp(t, dic2[mid].meaning) < 0) { high = mid-1; } else if(strcmp(t, dic2[mid].meaning) > 0) { low = mid+1; } else { int i=0; while(dic2[mid].order[i] != '\0') { putchar(dic2[mid].order[i]); i++; } putchar('\n'); return ; } } printf("what?\n"); } void Qury() { sort(dic, dic+len, cmp1); sort(dic2, dic2+len, cmp2); char t[100]; int n; scanf("%d", &n); getchar(); while(n-->0) { gets(t); //printf("Find:%s\n", t); if(t[0] == '[') { char order[100]; int i,j; for(i=1, j=0; t[i] != ']'; i++, j++) { order[j] = t[i]; } order[j] = '\0'; // printf("order:%s\n", order); Find1(order); }// order else { Find2(t); } } } int main() { #ifdef ONLINE_JUDGE #else freopen("E:\\in.txt", "r", stdin); #endif Init(); Input(); Qury(); return 0; }
相关文章推荐
- List<T>模糊查找、排序(非Linq)
- <九度 OJ>题目1061:成绩排序
- 九度:1034<部分排序><乌镇大富翁>
- 九度:1007<排序><奥运排序>
- 九度OJ 1029:魔咒词典 (排序)
- 九度OJ 1029:魔咒词典 (排序)
- List<T>采用delegate快速实现排序、查找等操作
- List<T>采用delegate快速实现排序、查找等操作
- List<T>采用delegate快速实现排序、查找等操作
- 查找<a>标签里的东西
- List<T> 排序(Sort)查找(Find)功能的多种实现
- C# List<T>.Sort排序
- 【C语言训练】排序问题<1>
- 九度:1016<火星A+B>
- 【Qt编程】基于Qt的词典开发系列<九>--JSON数据解析
- 对ArrayList<HashMap<String, String>>排序
- 九度:1045<百鸡问题><枚举>
- Gridview 绑定泛型List<T> 排序方法
- <菜鸟学算法-A排序(分治的思想:归并排序)>