经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C » 查看文章
【志银】NYOJ《题目490》翻译
来源:cnblogs  作者:志银  时间:2018/11/6 10:17:51  对本文有异议

1.题目:翻译

1.1.题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=490

1.2.题目内容

2.解题分析

题目输入输出格式描述不清晰。

2.1.分析(1)

所有数据都完成输入,然后再输出(解法1,内存可能不够(AC通过))

2.2.分析(2)

待测数据输完一行立马输出一行结果(解法2,内存够(没通过))

2.3.总结

两种写法都写了,最后以第一种输入输出格式通过,后台没有内存超出的数据

3.解题代码

3.1.解法1(AC)

  1. //解法1,内存可能不够,对题意通用性高(AC通过)
  2. #include<iostream>
  3. #include<map>
  4. #include<cstdio>
  5. using namespace std;
  6. int main() {
  7. string s[3005], s0, s1 = "", s2 = "";
  8. map<string, string> f;
  9. f["czy"] = "cml";
  10. cin >> s1;
  11. while(s2 != "BEGIN") {
  12. cin >> s1 >> s2;
  13. f[s2] = s1;
  14. }
  15. int n = 0;
  16. char ch[3005];
  17. do {
  18. cin >> s[++n];
  19. ch[n] = getchar();
  20. }while(s[n] != "END");
  21. for(int i = 1; i < n; i++) {
  22. s0 = "";
  23. for(int j = 0; j < s[i].size(); j++) {
  24. if(s[i][j] >= 'a' && s[i][j] <= 'z') {
  25. s0 += s[i][j];
  26. } else {
  27. if(f[s0] != "") cout << f[s0];
  28. else cout << s0;
  29. cout << s[i][j];
  30. s0 = "";
  31. }
  32. }
  33. if(f[s0] != "") cout << f[s0];
  34. else cout << s0;
  35. if(ch[i] == '\n') cout << "\n";
  36. else cout << " ";
  37. }
  38. }

3.2.解法2(WA)

  1. //解法2,内存能够,因为题意有歧义可能不能这样解(没通过)
  2. #include<iostream>
  3. #include<map>
  4. #include<cstdio>
  5. #include<cstring>
  6. using namespace std;
  7. int main() {
  8. string s0, s1 = "", s2 = "";
  9. map<string, string> f;
  10. f["czy"] = "cml";
  11. cin >> s1;
  12. while(1) {
  13. cin >> s1 >> s2;
  14. if(s2 == "BEGIN") break;
  15. f[s2] = s1;
  16. }
  17. char s[3005];
  18. getchar();
  19. while(1) {
  20. gets(s);
  21. if(s[0] == 'E' && s[1] == 'N' && s[2] == 'D') break;
  22. s0 = "";
  23. for(int i = 0; i < strlen(s); i++) {
  24. if(s[i] >= 'a' && s[i] <= 'z') {
  25. s0 += s[i];
  26. } else {
  27. if(f[s0] != "") cout << f[s0];
  28. else cout << s0;
  29. cout << s[i];
  30. s0 = "";
  31. }
  32. }
  33. cout << endl;
  34. }
  35. }

创建日期:2016.09.30

更新日期:2018.11.05

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号