| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
哪位大哥看看我的程序错在哪里,自认为木有错误的,N多例子都过了,一发就是WA,郁闷啊//顺便问一句,可不可以用sring之类的库函数,一个字符串比较函数都要自己写
#include <iostream>
using namespace std ;
int Flag = 0 ;
int MyCmp( const char * Str1 , const char * Str2 )//比较字符串(按字典序)
{
for ( int i = 0 ; i < 7 ; i ++ )
{
if ( Str1[i] > Str2[i] )
return 1 ;
else if ( Str1[i] < Str2[i] )
return -1 ;
}
return 0 ;
}
void MyCop( const char * Str1 , char * Str2 )//复制字符串
{
for ( int i = 0 ; Str1[i] != 0 ; i ++ )
Str2[i] = Str1[i] ;
Str2[i] = 0 ;
}
class Node //节点类
{
public :
Node() ;
~Node() ;
char Str[9] ;
long Times ;
Node * leftchild ;
Node * rightchild ;
} ;
Node::Node()
{
Times = 1 ;
leftchild = rightchild = NULL ;
}
Node::~Node()
{
}
class Tree//BST类
{
private:
void Delete( Node * cpNode ) ;
void TreatSub( char * ObjectStr , Node * & cpNode ) ;
void DisplaySub( Node * cpNode) ;
public :
Tree() ;
~Tree() ;
Node * cpRoot ;
void Treat( char * ObjectStr ) ;
void Display() ;
} ;
Tree::Tree()
{
cpRoot = NULL ;
}
Tree::~Tree()
{
Delete( cpRoot ) ;
}
void Tree::Delete( Node * cpNode ) //删除cpNode作为根节点的子树
{
if ( cpNode == NULL )
return ;
Delete( cpNode->leftchild ) ;
Delete( cpNode->rightchild ) ;
delete cpNode ;
}
void Tree::TreatSub( char * ObjectStr , Node * & cpNode )
{
if ( cpNode == NULL )
{
cpNode = new Node ;
MyCop( ObjectStr , cpNode->Str ) ;
return ;
}
if ( MyCmp( ObjectStr , cpNode->Str ) == 0 )
cpNode->Times ++ ;
else if ( MyCmp( ObjectStr , cpNode->Str ) < 0 )
TreatSub( ObjectStr , cpNode->leftchild) ;
else
TreatSub( ObjectStr , cpNode->rightchild ) ;
}
void Tree::Treat( char * ObjectStr )//将新的字符串插入BST
{
if ( this->cpRoot == NULL )
{
this->cpRoot = new Node ;
MyCop( ObjectStr , this->cpRoot->Str ) ;
return ;
}
if ( MyCmp( ObjectStr , this->cpRoot->Str ) == 0 )
this->cpRoot->Times ++ ;
else if ( MyCmp( ObjectStr , this->cpRoot->Str ) < 0 )
TreatSub( ObjectStr , this->cpRoot->leftchild) ;
else
TreatSub( ObjectStr , this->cpRoot->rightchild ) ;
}
void Tree::DisplaySub( Node * cpNode )
{
if ( cpNode == NULL )
return ;
DisplaySub( cpNode->leftchild ) ;
if ( cpNode->Times > 1 )
{
Flag = 1 ;
for ( int i = 8 ; i > 3 ; i -- )
cpNode->Str[i] = cpNode->Str[i-1] ;
cpNode->Str[3] = '-' ;
cout << cpNode->Str << ' ' << cpNode->Times << endl ;
}
DisplaySub( cpNode->rightchild ) ;
}
void Tree::Display()//打印
{
if ( cpRoot == NULL )
return ;
DisplaySub( cpRoot->leftchild ) ;
if ( cpRoot->Times > 1 )
{
Flag = 1 ;
for ( int i = 8 ; i > 3 ; i -- )
cpRoot->Str[i] = cpRoot->Str[i-1] ;
cpRoot->Str[3] = '-' ;
cout << cpRoot->Str << ' ' << cpRoot->Times << endl ;
}
DisplaySub( cpRoot->rightchild ) ;
}
void main()
{
int i ;
long Case ;
char Temp[30] ;
Tree aTree ;
cin >> Case ;
while((Case--) != 0 )//处理
{
cin >> Temp ;//输入
for ( i = 0 ; Temp[i] != 0 ; i ++ )//去掉'-'
{
if ( Temp[i] == '-' )
{
for ( int j = i ; Temp[j] != 0 ; j ++ )
Temp[j] = Temp[j+1] ;
i -- ;
}
}
for ( i = 0 ; Temp[i] != 0 ; i ++ )//转换成数字(字符)形式
{
if ( Temp[i] >= 'A' && Temp[i] <= 'C' )
Temp[i] = '2' ;
else if ( Temp[i] >= 'D' && Temp[i] <= 'F' )
Temp[i] = '3' ;
else if ( Temp[i] >= 'G' && Temp[i] <= 'I' )
Temp[i] = '4' ;
else if ( Temp[i] >= 'J' && Temp[i] <= 'L' )
Temp[i] = '5' ;
else if ( Temp[i] >= 'M' && Temp[i] <= 'O' )
Temp[i] = '6' ;
else if ( Temp[i] >= 'P' && Temp[i] <= 'S' )
Temp[i] = '7' ;
else if ( Temp[i] >= 'T' && Temp[i] <= 'V' )
Temp[i] = '8' ;
else if ( Temp[i] >= 'W' && Temp[i] <= 'Y' )
Temp[i] = '9' ;
else ;
}
aTree.Treat( Temp ) ;//插入BST
}
aTree.Display() ;//打印
if ( Flag == 0 )//如果没有重复的,打印
cout << "No duplicates." ;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator