请各位高手帮我改善下这个程序的效率,谢谢大家了

tenglvjun 2008-03-31 05:13:49
#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
using namespace std;

//--------------------------------------
//------------去处'-'符号---------------
bool whither(vector<string>& vect)
{
for(vector<string>::iterator iter = vect.begin(); iter != vect.end(); iter++)
{
string::iterator it = remove((*iter).begin(), (*iter).end(), '-');
(*iter).erase(it, (*iter).end());
}
return true;
}

//--------------------------------------
//---------------test Number------------
bool IsNum(char a)
{
switch(a)
{
case '0':
return true;
break;
case '1':
return true;
break;
case '2':
return true;
break;
case '3':
return true;
break;
case '4':
return true;
break;
case '5':
return true;
break;
case '6':
return true;
break;
case '7':
return true;
break;
case '8':
return true;
break;
case '9':
return true;
break;
}
return false;
}

//--------------------------------------
//----------字母转换成数字--------------
bool transform(vector<string>& vect, map<char, char>& table)
{
for(vector<string>::iterator iter = vect.begin(); iter != vect.end(); iter++)
{
for(string::size_type pos = 0; pos != (*iter).length(); pos++)
{
if(!IsNum((*iter).at(pos)))
(*iter)[pos] = table[(*iter).at(pos)];
}
}
return true;
}

//--------------------------------------
//--------------计数--------------------
bool counter(vector<string>& vect, map<string, int>& con)
{
vector<string>::iterator iter = vect.begin();
while(iter != vect.end())
{
int count = 1;
vector<string>::iterator iter1 = iter + 1;
while(iter1 != vect.end())
{
if((*iter) == (*iter1))
{
count++;
iter1 = vect.erase(iter1);
continue;
}
iter1++;
}
(*iter).insert(3, "-");
con.insert(make_pair(*iter, count));
iter++;
}
return true;
}

int main()
{
fstream cin("1.txt");
int count = 0;

//---------------------------------------
//--------------初始化MAP表--------------
map<char, char> table;
map<string, int> con;
table.insert(make_pair('A', '2'));
table.insert(make_pair('B', '2'));
table.insert(make_pair('C', '2'));
table.insert(make_pair('D', '3'));
table.insert(make_pair('E', '3'));
table.insert(make_pair('F', '3'));
table.insert(make_pair('G', '4'));
table.insert(make_pair('H', '4'));
table.insert(make_pair('I', '4'));
table.insert(make_pair('J', '5'));
table.insert(make_pair('K', '5'));
table.insert(make_pair('L', '5'));
table.insert(make_pair('M', '6'));
table.insert(make_pair('N', '6'));
table.insert(make_pair('O', '6'));
table.insert(make_pair('P', '7'));
table.insert(make_pair('R', '7'));
table.insert(make_pair('S', '7'));
table.insert(make_pair('T', '8'));
table.insert(make_pair('U', '8'));
table.insert(make_pair('V', '8'));
table.insert(make_pair('W', '9'));
table.insert(make_pair('X', '9'));
table.insert(make_pair('Y', '9'));

//------------------------------------------
//-------------正程序-----------------------
vector<string> vect;
string str;
for(int n; cin >> n;)
{
while(n != 0)
{
cout << endl;
n--;
}
int N;
cin >> N;
while(N != 0)
{
cin >> str;
vect.push_back(str);
N--;
}

whither(vect);
transform(vect, table);
counter(vect, con);
sort(vect.begin(), vect.end());
for(map<string, int>::iterator iter = con.begin(); iter != con.end(); iter++)
{
if(iter->second == 1)
{
count++;
continue;
}
else
cout << iter->first << " " << iter->second << endl;
}
if(count == vect.size())
cout << "No duplicates." << endl;
}
return 0;
}

1.txt
1

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279
...全文
115 8 打赏 收藏 举报
写回复
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tenglvjun 2008-04-01
  • 打赏
  • 举报
回复
#include<iostream>
#include<fstream>
#include<algorithm>
#include<string>
using namespace std;

class BSTreeNode
{
public:
BSTreeNode()
{
phoneName = 0;
times = 1;
left = right = NULL;
}

BSTreeNode(long name)
{
phoneName = name;
times = 1;
left = right = NULL;
}
BSTreeNode& operator=(BSTreeNode& rhs)
{
if(this == &rhs)
return *this;
phoneName = rhs.phoneName;
times = rhs.times;
return *this;
}
long phoneName;
int times;
BSTreeNode *left;
BSTreeNode *right;
};

long convert(string str)
{
long temp = 0;
for(string::iterator iter = str.begin(); iter != str.end(); iter++)
{
if(*iter != '-')
{
temp = temp * 10;
switch(*iter)
{
case'1':temp+=1;break;
case'A':case'B':case'C':case'2': temp += 2;break;
case'D':case'E':case'F':case'3': temp += 3;break;
case'G':case'H':case'I':case'4': temp += 4;break;
case'J':case'K':case'L':case'5': temp += 5;break;
case'M':case'N':case'O':case'6': temp += 6;break;
case'P':case'R':case'S':case'7': temp += 7;break;
case'T':case'U':case'V':case'8': temp += 8;break;
case'W':case'X':case'Y':case'9': temp += 9;break;
default:break;
}
}
}
return temp;
}

void InOrderTraverse(BSTreeNode* t)
{
if(t != NULL)
{
InOrderTraverse(t->left);
if(t->times > 1)
cout << t->phoneName / 10000 << "-" << t->phoneName % 10000 << " " << t->times << endl;;
InOrderTraverse(t->right);
}
}


int main()
{
fstream cin("1.txt");

int blockNum;
string str;
cin >> blockNum;
while(blockNum != 0)
{
BSTreeNode *tree = NULL;
BSTreeNode *p, *f;
int count = 0;
int strNum;
cin >> strNum;
while(strNum != 0)
{
bool flage = true;
cin >> str;
long temp = convert(str);
p = tree;
while(p)
{
if(temp == p->phoneName)
{
p->times++;
count = 1;
flage = false;
}
f = p;
p = (temp - (*f).phoneName) < 0 ? p->left : p->right;
}
if(flage)
{
p = new BSTreeNode(temp);
if(tree == NULL)
tree = p;
else if((temp - (*f).phoneName) < 0)
{
f->left = p;
}
else if((temp - (*f).phoneName) > 0)
{
f->right = p;
}
}
strNum--;
}
if(count)
InOrderTraverse(tree);
else
cout << "No duplicates." << endl;
if(blockNum - 1 != 0)
cout << endl;
blockNum--;
}
return 0;
}
各位高手,我已经把我自己的代码给改善了下,用了树,把字符串也转换成了数,可是还是超时,请各位帮我看看啊,谢谢大家了
lily604 2008-03-31
  • 打赏
  • 举报
回复
判断是否是数字还可用
std::isdigit()
lily604 2008-03-31
  • 打赏
  • 举报
回复
如果功能和你贴的对应的那部分代码一样,可以改为如下
bool IsNum(char a)
{
switch(a)
{
case '0':

case '1':

case '2':

case '3':

case '4':

case '5':

case '6':

case '7':

case '8':

case '9':
return true;
break;


}
return false;
}
野男孩 2008-03-31
  • 打赏
  • 举报
回复
呵呵~~这道题我以前做过,测试数据出了名的变态,提交了3次才AC。


#include <iostream>
#include <string>
using namespace std;

char chMaps[] = {'2','2','2','3','3','3','4','4','4','5','5','5'
,'6','6','6','7','7','7','7','8','8','8','9','9','9','9'};

class BTree
{
public:
BTree(const char* szVal = "")
{
Val = szVal;
nCnt = Val.empty() ? 0:1;
pLeft = pRight = NULL;
}

int Insert(const char* szVal)
{
int nCmpRet = strcmp(szVal, Val.c_str());
if (nCmpRet > 0)
{
if (pRight)
{
return pRight->Insert(szVal);
}
else
{
pRight = new BTree(szVal);
return 1;
}
}
else if (nCmpRet < 0)
{
if (pLeft)
{
return pLeft->Insert(szVal);
}
else
{
pLeft = new BTree(szVal);
return 1;
}
}
else
{
return ++nCnt;
}
}

void InOrder()
{
if (pLeft)
{
pLeft->InOrder();
}
if (nCnt > 1)
{
cout << Val << " " << nCnt << endl;
}
if (pRight)
{
pRight->InOrder();
}
}
private:
string Val;
int nCnt;
BTree *pLeft;
BTree *pRight;
};

int main()
{
int nLines = 0;
cin>>nLines;

if (nLines <= 0)
{
cout<<"No duplicates."<<endl;
return 0;
}

BTree *pTree = NULL;
int nCnt = 0;
bool bNoDuplicate = true;
do
{
char buf[128] = {0};
char tmp[10] = {0};
cin>>buf;

int nPos = 0;
for(int i=0; buf[i]!=0; i++)
{
if (buf[i] == '-')
{
continue;
}

if (buf[i] < 'A')
{
tmp[nPos++] = buf[i];
}
else
{
if (buf[i] == 'Q' || buf[i] == 'Z')
{
break;
}
tmp[nPos++] = chMaps[buf[i] - 'A'];
}
if (nPos == 3)
{
tmp[nPos++] = '-';
}
else if (nPos == 8)
{
if (pTree == NULL)
{
pTree = new BTree(tmp);
}
else
{
if (pTree->Insert(tmp) > 1)
{
bNoDuplicate = false;
}
}
break;
}
}
}while(--nLines > 0);

if (bNoDuplicate)
{
cout<<"No duplicates."<<endl;
}
else
{
pTree->InOrder();
}

return 0;
}
homesos 2008-03-31
  • 打赏
  • 举报
回复
改循环!
tenglvjun 2008-03-31
  • 打赏
  • 举报
回复
这个是道ACM题
题目是
Businesses like to have memorable telephone numbers. One way to make a telephone number memorable is to have it spell a memorable word or phrase. For example, you can call the University of Waterloo by dialing the memorable TUT-GLOP. Sometimes only part of the number is used to spell a word. When you get back to your hotel tonight you can order a pizza from Gino's by dialing 310-GINO. Another way to make a telephone number memorable is to group the digits in a memorable way. You could order your pizza from Pizza Hut by calling their ``three tens'' number 3-10-10-10.

The standard form of a telephone number is seven decimal digits with a hyphen between the third and fourth digits (e.g. 888-1200). The keypad of a phone supplies the mapping of letters to numbers, as follows:


A, B, and C map to 2

D, E, and F map to 3

G, H, and I map to 4

J, K, and L map to 5

M, N, and O map to 6

P, R, and S map to 7

T, U, and V map to 8

W, X, and Y map to 9


There is no mapping for Q or Z. Hyphens are not dialed, and can be added and removed as necessary. The standard form of TUT-GLOP is 888-4567, the standard form of 310-GINO is 310-4466, and the standard form of 3-10-10-10 is 310-1010.


Two telephone numbers are equivalent if they have the same standard form. (They dial the same number.)


Your company is compiling a directory of telephone numbers from local businesses. As part of the quality control process you want to check that no two (or more) businesses in the directory have the same telephone number.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Input

The input will consist of one case. The first line of the input specifies the number of telephone numbers in the directory (up to 100,000) as a positive integer alone on the line. The remaining lines list the telephone numbers in the directory, with each number alone on a line. Each telephone number consists of a string composed of decimal digits, uppercase letters (excluding Q and Z) and hyphens. Exactly seven of the characters in the string will be digits or letters.



Output

Generate a line of output for each telephone number that appears more than once in any form. The line should give the telephone number in standard form, followed by a space, followed by the number of times the telephone number appears in the directory. Arrange the output lines by telephone number in ascending lexicographical order. If there are no duplicates in the input print the line:

No duplicates.


Sample Input

1

12
4873279
ITS-EASY
888-4567
3-10-10-10
888-GLOP
TUT-GLOP
967-11-11
310-GINO
F101010
888-1200
-4-8-7-3-2-7-9-
487-3279


Sample Output

310-1010 2
487-3279 4
888-4567 3


我已经实现了,可是提交的时候它说我运行超时,时间限制是10 Seconds;可是我自己电脑上运行的时候速度很快,根本没有10 Seconds ,我也不知道这个该怎么改,请各位大哥帮我提点意见,小弟刚学的,谢谢大家啊
rushman 2008-03-31
  • 打赏
  • 举报
回复
程序没有说明,没头没脑的,看的很累……
donwmufromdying 2008-03-31
  • 打赏
  • 举报
回复
如果你是以查找或者插入为主的请用List代替vector,map也可以用hashmap
相关推荐
发帖
C++ 语言

6.3w+

社区成员

C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
帖子事件
创建了帖子
2008-03-31 05:13
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下