标签:
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=490
这题的输入输出格式好像描述的不太清楚,
1)可能是所有数据都完成输入,然后再输出(解法1,内存可能不够,对题意通用性高(AC通过))
2)也可能是待测试的数据输完一行就立马输出一行结果(解法2,内存能够,因为题意有歧义可能不能这样解(没通过))
两种写法都写了,最后以第一种输入输出格式通过的,还好后台数据没有内存超出的
下面贴上代码:
解法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 }
解法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.9.30 ----志银
标签:
原文地址:http://www.cnblogs.com/chenzhiyin/p/5922483.html