浙大1159,超时

radioheads 2002-11-05 11:43:17
做出来的请指导一下
...全文
110 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
radioheads 2002-11-14
  • 打赏
  • 举报
回复
我用的就是map,超时了
using namespace std;
#define DEBUG
#ifdef DEBUG
#include<fstream>
ifstream infile("1159.in");
ofstream outfile("1159.out");
istream& in(infile);
ostream& out(outfile);
#else
#include<iostream>
istream& in(cin);
ostream& out(cout);
#endif
#include<map>
#include<string>

bool flag;
void deal(string& str) // 转换成统一格式
{
string tmp = str;
str ="";

for(int ix = 0; ix < tmp.size(); ix++)
{
if(str.size() == 3)
{
str.push_back('-');
}
if(tmp[ix] == 'A' || tmp[ix] == 'B' || tmp[ix] == 'C')
{
str.push_back('2');
continue;
}
if(tmp[ix] == 'D' || tmp[ix] == 'E' || tmp[ix] == 'F')
{
str.push_back('3');
continue;
}
if(tmp[ix] == 'G' || tmp[ix] == 'H' || tmp[ix] == 'I')
{
str.push_back('4');
continue;
}
if(tmp[ix] == 'J' || tmp[ix] == 'K' || tmp[ix] == 'L')
{
str.push_back('5');
continue;
}
if(tmp[ix] == 'M' || tmp[ix] == 'N' || tmp[ix] == 'O')
{
str.push_back('6');
continue;
}
if(tmp[ix] == 'P' || tmp[ix] == 'R' || tmp[ix] == 'S')
{
str.push_back('7');
continue;
}
if(tmp[ix] == 'T' || tmp[ix] == 'U' || tmp[ix] == 'V')
{
str.push_back('8');
continue;
}
if(tmp[ix] == 'W' || tmp[ix] == 'X' || tmp[ix] == 'Y')
{
str.push_back('9');
continue;
}
if(int(tmp[ix]) > 47 && int(tmp[ix]) < 59)
{
str.push_back(tmp[ix]);
}
}
}
void solve()
{
map<string, long> data;
int m;
in >> m;
for(int ix = 0; ix < m; ix++)
{
string str;
in >> str;

deal(str);//转换格式
data[str]++;// 记数
}
if(data.size()==m)
{
flag = false;
return;
}
map<string, long>::iterator ix = data.begin();
for(; ix != data.end(); ix++)
{
int t = (*ix).second;
if(t > 1)
out << (*ix).first <<" " <<t<< endl;


}
}

int main()
{
int n;
in >> n;
while(n--)
{
flag = true;
solve();
if(!flag)
out <<"No duplicates."<<endl;
if(n != 0)
out << endl;
}
}

starfish 2002-11-14
  • 打赏
  • 举报
回复
TO: leojay
vector内部使用了称之为动态表的数据结构实现
在"平摊分析"的平均意义下动态表中每插入和删除一个元素需要进行3次赋值操作
这个代价并不算很大
具体的分析可以看C++ View上面我以前写过的一篇文章。
alidiedie 2002-11-14
  • 打赏
  • 举报
回复
我开始做的程序也超时,没有用map ,但用了类似与集合的结构,每得到一个新的电话号码,就去已经得到的电话本中查找,如果这个telno是第一次出现,就放进集合,个数置1,如果电话本中已经有了该telno ,就个数加1.
提交上去老是超时.你用了map ,估计在map[str]++这个操作中也有类似于查表的操作,所以就超时了.
海星的程序比较巧妙,他没有查表的操作,只有一个排序.再遍历一遍就好了.
leojay 2002-11-14
  • 打赏
  • 举报
回复
你把你编译通过的程序贴上来,我帮你看一下。
leojay 2002-11-14
  • 打赏
  • 举报
回复
你的if太多,写成switch会好一些
如:
inline char alpha2num( char c )
{
switch( c )
{
case 'A':case 'B':case 'C': return '2' ;
case 'D':case 'E':case 'F': return '3' ;
case 'G':case 'H':case 'I': return '4' ;
case 'J':case 'K':case 'L': return '5' ;
case 'M':case 'N':case 'O': return '6' ;
case 'P':case 'R':case 'S': return '7' ;
case 'T':case 'U':case 'V': return '8' ;
case 'W':case 'X':case 'Y': return '9' ;
}
return '?' ;
}
leojay 2002-11-14
  • 打赏
  • 举报
回复
你的贴上来的程序通过编译了吗?
在我这通不过编译,
using namespace std;
#define DEBUG
#ifdef DEBUG
#include<fstream>
ifstream infile("1159.in");
ofstream outfile("1159.out");
istream& in(infile);
ostream& out(outfile);
#else
#include<iostream>
istream& in(cin);
ostream& out(cout);
#endif
#include<map>
#include<string>
这一串东西里就有错,
using namespace std;不可以在第一行的,
std 是在iostream里声明的你怎么可以把他放在最上面呢?


还有,你的deal函数里的那些str.push_back('7');你确定没有错吗?
string也有push_back函数??

另外(*ix).first 写成ix->first 就可以了。
leojay 2002-11-13
  • 打赏
  • 举报
回复
关于 starfish(海星)的程序,我想就vector跟starfish(海星)探讨
一下,STL中我个人认为做得最不好的就是vector,每次作插入的时候,
当总的大小大于2^n的时候,vector会重新申请一个大小为2^(n+1)的空间,
然后把以前的那2^n的数据全部copy过来,当数据大的时候,
这样就会慢了很多。
所以我更愿意用数组,开到足够大就是了。

我在ZJU上看starfish用了16.25秒,我想很大的程度上是vector的原因。

这题我用的是map<string,int>,这样的话,就可以不用sort,不会有vector
那样的大量数据的copy,速度就会快一些。
starfish 2002-11-08
  • 打赏
  • 举报
回复
//#define DEBUG

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

#ifdef DEBUG
#include <fstream>
#endif

using namespace std;

#ifdef DEBUG
ifstream fin("1159.in");
ofstream fdebug("debug.txt");
istream& in = fin;
ostream& out = cout;
#else
istream& in = cin;
ostream& out = cout;
#endif

vector<string> numbers;


char Transform(char x)
{
if ( (x >= '0') && (x <= '9') ) return x;
if ( (x >= 'A') && (x <= 'C') ) return '2';
if ( (x >= 'D') && (x <= 'F') ) return '3';
if ( (x >= 'G') && (x <= 'I') ) return '4';
if ( (x >= 'J') && (x <= 'L') ) return '5';
if ( (x >= 'M') && (x <= 'O') ) return '6';
if ( (x >= 'P') && (x <= 'S') ) return '7';
if ( (x >= 'T') && (x <= 'V') ) return '8';
if ( (x >= 'W') && (x <= 'Y') ) return '9';

return '?';
}

string StandardForm(string str)
{
string result = "";

for (int i = 0; i < str.length(); i++) {
if (str[i] != '-') {
char ch = Transform(str[i]);
result.append(1, ch);
}
}

result.insert(3, "-");

return result;
}

int main()
{
int t, n;
string str;

in >> t;
for (int i = 0; i < t; i++) {
if (i > 0) out << endl;

in >> n;
numbers.clear();

for (int i = 0; i < n; i++) {
in >> str;
numbers.push_back(StandardForm(str));
}

sort(numbers.begin(), numbers.end());

#ifdef DEBUG
fdebug << "The numbers are : " << endl;
copy(numbers.begin(), numbers.end(), ostream_iterator<string>(fdebug, "\n"));
fdebug << endl;
#endif

str = "XXX";
int occur = 1;
bool duplicate = false;
for (int i = 0; i < numbers.size(); i++) {
if (numbers[i] != str) {
if (occur > 1) out << occur << endl;
occur = 1;
str = numbers[i];
} else {
occur++;
duplicate = true;
if (occur == 2) out << numbers[i] << " ";
}
}
if (occur > 1) out << occur << endl;

if (!duplicate) out << "No duplicates." << endl;

}
return 0;
}

33,028

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧