这题因为用getline读一直WA,直接导致我将近一天不想做题。。。。。。。。
话说为毛不能用getline啊,可能是我还有别的问题,但我真的是检查了好几遍了,虽然时间快接近2000ms了,但并不是超时,而是WA啊。
我真的不想用get,因为lrj老师说的原因,而且每次还要用次getchar(),但我tm真的没法子了。
哎,自己的困难终究还是要自己克服。。。
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <ctype.h>
#include <cctype>
#include <string.h>
using namespace std;
const int N = 3010;
const int INF = 10000000;
map <string, string> M;
int main()
{
// freopen("in.txt", "r", stdin);
string s0, key, val;
cin >> s0;
while(1)
{
cin >> key;
if(!key.compare("END")) break;
cin >> val;
M[val] = key;
}
map <string, string> :: iterator it;
cin >> s0;
getchar();
char line[N];
while(1)
{
gets(line);
if(!strcmp(line, "END")) break;
int len = strlen(line);
string word;
for(int i = 0; i < len; i ++)
{
if(isalpha(line[i]))
{
word += line[i];
}
else
{
if(M.count(word))
{
it = M.find(word);
word = it -> second;
cout << word;
}
else cout << word;
word.clear();
cout << line[i];
}
}
cout << endl;
}
return 0;
}
失败的getline:
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <ctype.h>
#include <cctype>
#include <string.h>
using namespace std;
const int N = 3010;
const int INF = 10000000;
map <string, string> M;
int main()
{
// freopen("in.txt", "r", stdin);
string s0, key, val, line, buf, buf0;
cin >> s0;
while(1)
{
cin >> key;
if(!key.compare("END")) break;
cin >> val;
M[val] = key;
}
map <string, string> :: iterator it;
cin >> s0;
while(1)
{
getline(cin, line);
if(line == "") continue;
if(!line.compare("END")) break;
stringstream s0(line);
int n = 0;
while(s0 >> buf0)
{
n ++;
}
int n0 = 0;
stringstream ss(line);
while(ss >> buf)
{
n0 ++;
int flag = 0;
int len = buf.size();
char c[N];
if(!isalpha(buf[len - 1]))
{
for (int i = 0; i < len; i ++)
{
c[i] = buf[i];
}
buf.erase(len - 1);
flag = 1;
}
if(M.count(buf))
{
it = M.find(buf);
buf = it -> second;
}
cout << buf;
if(flag) printf("%c", c[len - 1]);
if(n0 != n) printf(" ");
}
printf("\n");
}
return 0;
}