USACO-Section1. 4000 3 Name That Number (遍历与字符串比较)
2017-05-27 09:31
375 查看
2017-5-26
题目描述
解答
代码
需要注意的几点:
(1)读取dict.txt如果定义全局的,那么每次是从上一次结束的地方继续读取的,如果是在函数里,那么每次都是从头开始读取的。
(2)如果每次从头开始读取,是会造成超时的。
(3)如果每次都从之前的地方继续读取的话,可能会存在读取的比判断的快,此时我们需要记住上一次读取的字符串tmp,如果它的字典序大于要判断的那个字符串的话,那么我们就不要进入循环进行读取了。
题目描述
大概就是将字符组合,判断它是否在给定的文件中
解答
这道题字符组合我用的是深度搜索,我在查找时只打开了一次文件, 每次继续上次的查找位置查找,因为我们求出来的字符组合本身就是有序的。 需要注意的是得回去一行。
代码
/* ID: 18795871 PROG: namenum LANG: C++ */ #include<iostream> #include<fstream> #include<cstring> #include<cstdio> using namespace std; ifstream fin1("dict.txt"); ifstream fin("namenum.in"); ofstream fout("namenum.out"); const int N = 12; char x[8][4]={"ABC","DEF","GHI","JKL","MNO","PRS","TUV","WXY"}; char s[N+1],r[N+1],str2[N+1]="\0"; int l,flag=0; int b=0; bool res(){ char str1[N+1]; if (strcmp(str2,r)==0) return true; if (strcmp(str2,r)>0) return false; while (!fin1.eof()){ fin1>>str1; strcpy(str2,str1); b++; if (strcmp(r,str1)==0) return true; if (strcmp(str1,r)>0) return false; } } void dfs(int step){ if (step==l){ r[l]='\0'; if (res()){ flag=1; fout<<r<<endl; } return ; } for (int i=0;i<3;i++){ r[step]=x[s[step]-'0'-2][i]; dfs(step+1); } } int main() { char c; l=0; while(fin>>c){ if (c=='\n') break; s[l++]=c; } dfs(0); if (!flag) fout<<"NONE"<<endl; return 0; }
需要注意的几点:
(1)读取dict.txt如果定义全局的,那么每次是从上一次结束的地方继续读取的,如果是在函数里,那么每次都是从头开始读取的。
(2)如果每次从头开始读取,是会造成超时的。
(3)如果每次都从之前的地方继续读取的话,可能会存在读取的比判断的快,此时我们需要记住上一次读取的字符串tmp,如果它的字典序大于要判断的那个字符串的话,那么我们就不要进入循环进行读取了。
/* ID: 18795871 PROG: namenum LANG: C++ */ #include<iostream> #include<fstream> #include<cstring> using namespace std; ifstream fin("namenum.in"); ofstream fout("namenum.out"); ifstream din("dict.txt"); const int N = 12; char cmp[8][3]={'A','B','C','D','E','F','G','H','I','J','K', 'L','M','N','O','P','R','S','T','U','V','W','X','Y'}; char x[N+1],r[N+1],tmp[N+1]; int n; bool flag; bool isCompare(){ if (!strcmp(tmp,r)) return true; if (strcmp(tmp,r)>0) return false; char s[N+1]; while (din>>s){ if (strcmp(s,r)==0){ return true; } if (strcmp(s,r)>0){ strcpy(tmp,s); return false; } } } void dfs(int step){ if (step==n){ r ='\0'; if (isCompare()){ flag=true; for (int i=0;i<n;i++){ fout<<r[i]; } fout<<endl; } return ; } for (int i=0;i<3;i++){ r[step]=cmp[x[step]-'0'-2][i]; dfs(step+1); } } int main(){ while (fin>>x){ flag=false; n=strlen(x); dfs(0); if (!flag) fout<<"NONE"<<endl; } return 0; }
相关文章推荐
- USACO-section1.2.3 Name that number
- USACO Section 1.2 Name That Number 解题报告
- USACO Section 1.2 : Name That Number
- USACO Section 1.2 Name That Number
- USACO之Section1.2.3 Name That Number
- USACO section1.2 Name That Number
- USACO-Section1.2 Name That Number [其他]
- USACO-Section1.2 Name That Number【暴力枚举】【二分查找】
- USACO Section 1.2 Name That Number
- USACO-Section 1.2 Name That Number[...]
- USACO Section 1.2 Name That Number
- USACO Section1.2 Name That Number 解题报告
- usaco Name That Number
- usaco Name That Number
- [USACO]Name That Number
- USACO Name That Number (dfs+字典树)
- 【USACO-Chapter1-1.2】【模拟】Name That Number
- usaco 1.2.3 name that number
- USACO Solution Code(5):Name That number, Palindromic Squares and Dual Palindromes
- Usaco 1.2.3 命名那个数字(Name That Number)