源代码如下~我用题目本身的数据测试过~答案一样~但是我尝试提交却说我WA~肿么破~大家可以复制过去测试下啊
#include<iostream>
#include<string>
using namespace std;
void Solve(string & s,string *list,int N,int M)
{
int i,j,index1=0,index2=0,flag=0;
string s2,word;
for(i=0;i<N-1;i++)
{
for(;index1!=s.size()-1;)
{
index1=s.find(' ',index2);
word=s.substr(index2,index1-index2);
for(j=0;j<2*M;j+=2)
{
if(word==list[j])
{
index2=index2+list[j].size()+1;
s2+=list[j+1];
s2+=' ';
flag=1;
break;
}
}
if(flag!=1||index1==-1)
{
index2=index1+1;
s2+=word;
s2+=' ';
}
else
flag=0;
}
index2=0;
index1=0;
s=s2;
s2.clear();
}
}
int main()
{
string *list,*s;
char ch;
int T,N,M,i,j;
cin>>T;
s=new string[T];
for(i=0;i<T;i++)
{
cin>>N>>M;
list=new string[2*M];
for(j=0;j<2*M;j++)
cin>>list[j];
cin.get(ch);
getline(cin,s[i]);
s[i]+=' ';
Solve(s[i],list,N,M);
s[i]=s[i].erase(s[i].size()-1);
delete []list;
}
for(i=0;i<T;i++)
cout<<"case #"<<i+1<<": "<<s[i]<<endl;
}
#include <iostream> #include <map> #include <string> #include <queue> using namespace std; string findmappingword(map<string, string> mapwords, string word, int turns); int main(int argc, char **argv){ int case_num = 0; int people_num,mapping_num, num = 1; string real_word, map_word, sentence = ""; map<string, string> mapwords; queue<string> sentences; cin>>case_num; while(case_num>0){ sentence=""; mapwords.clear(); cin>>people_num>>mapping_num; while (mapping_num>0) { cin>>real_word>>map_word; mapwords.insert(pair<string,string>(real_word, map_word)); mapping_num--; } cin.clear(); cin.sync(); //cin.get(); //cin.clear(); //cin.getline(c_oldsentence, sizeof(c_oldsentence)); //string oldsentence(c_oldsentence); string oldsentence; getline(cin, oldsentence); string::size_type size = oldsentence.size(); string::size_type idx; for (string::size_type i=0; i<size; i++) { idx=oldsentence.find(" ", i); string s; if (idx == string::npos) { s = oldsentence.substr(i, size-1); i=size; } if(idx < size){ s=oldsentence.substr(i,idx-i); i=idx; } s=findmappingword(mapwords,s,people_num-1); sentence.append(s+" "); } sentences.push(sentence); case_num--; } while(!sentences.empty()){ cout<<"Case #"<<num++<<": "<<sentences.front()<<endl; sentences.pop(); } return 1; } string findmappingword(map<string, string> mapwords, string word, int turns){ map<string, string>::iterator iter; for (int i=0; i<turns; i++) { iter=mapwords.find(word); if(iter==mapwords.end()){ break; }else{ word=iter->second; } } return word; }
我的也是一样的情况,测试样例正确,wa,怎么破。
同是天涯沦落人~握爪。。。
膜拜一下
我这道题也写了,问题和你差不多,样例程序没问题,但是我的是时间超出。我很好奇啊,在code::blocks上做的好好地,怎么就时间超出了?我刚才故意做了几个测试,结果表明我在后面的随便输出根本就没有关系,依旧是时间超出。
我的代码,在codeblocks上输出样例正确
#include <iostream>
#include <cstring>
#include <vector>
#include <stdio.h>
using namespace std;
typedef struct Mymap
{
string pre;
string now;
}Map,* pMap;
vector<string> strVec;
int main()
{
int T;
int m, n;
char Buf[101];
cin >> T;
vector<string> * svBuf = new vector<string>[T];
for(int k = 0;k < T;k ++)
{
cin >> m >> n;
pMap strMap = new Map[n];
for(int i = 0;i < n;i ++)
{
cin >> strMap[i].pre >> strMap[i].now;
}
//cout << "******************";
cin.clear();
cin.sync();
//Big jop
cin.getline(Buf, 100);
int WordNumber = 0;
int PreAddr = 0;
string strTemp;
int strlength = strlen(Buf);
for(int i = 0; i < strlength; i ++)
{
if(Buf[i] != ' ')
{
if(i == strlength - 1)
{
strTemp.append(Buf + PreAddr, i - PreAddr + 1);
strVec.push_back(strTemp);
strTemp.clear();
break;
}
continue;
}
strTemp.append(Buf + PreAddr, i - PreAddr);
strVec.push_back(strTemp);
strTemp.clear();
PreAddr = i + 1;
WordNumber++;
}
//Big job end;
int Temp = m - 1;
while(Temp --)
for(unsigned int i = 0;i < strVec.size(); i ++)
for(int j = 0;j < n; j ++)
{
if(strVec[i].compare(strMap[j].pre) == 0)
{
strVec[i] = strMap[j].now;
break;
}
}
sprintf(Buf, "Case #%d:", k);
svBuf[k].push_back(Buf);
for(unsigned int i = 0; i < strVec.size(); i++)
svBuf[k].push_back(strVec[i]);
delete [] strMap;
strVec.clear();
}
for(int i = 0; i < T; i++)
{
for(unsigned int j = 0; j < svBuf[i].size(); j ++)
{
cout << svBuf[i][j] << ends;
}
cout << endl;
}
delete [] svBuf;
return 0;
}
@Matrix_R: 你的还好啦,我的直接来个wa,死得不明不白
@Mushroom0417:
刚才帮你看过了 你的代码里出现的问题在于你使用了 cin.get(ch); 去拦截'\n'。
在程序进行检测的时候这个符号貌似是不会出现的。
可以使用cin.clear()和cin.sync()清除空输出。
@Matrix_R: 根据你说的我把CIN.GET(CH)改为CIN.SYNC()~然后我在VS上也测试过没问题~然后我再去提交下看看~结果出了个RUNTIME ERROR~我对这个判断对错的程序无语了~看来是天在警告我别参加这些比赛~在塞外玩玩就好。。。
我看过你的代码了~也换成C++版来测试过~求解过程无误~时间也用计算了下符合啊。。奇葩了
@Mushroom0417: ACM就是这样,我经常郁闷~~~原本以为编程之美是考一些比较新鲜的算法问题的~~~没成想啊~
真的奇葩了~