d518.
文字抄寫 II
| From: [125.224.124.226] |
發表日期
:
2010-12-28 19:50
#include <iostream>
#define G getchar()
using namespace std;
struct Tree{
short num;
struct Tree* branch[26];
Tree(){
num=0;
memset(branch, 0, sizeof(Tree*)*26);
}
};
inline void free(Tree* p);
inline void charget(char* temp);
inline void free(Tree* p){
for(int i=0; i<26; ++i) if(p->branch[i]) free(p->branch[i]);
delete p;
}
inline void charget(char* temp){
memset(temp, 0, 21*sizeof(char));
short count=0;
char ch;
while(ch=G) if(ch!='\n' && ch!=' ') break;
temp[count++]=ch;
while(ch=G){
if(ch=='\n' || ch==' ' || ch==EOF) break;
else temp[count++]=ch;
}
}
int main(){
short asks;
while(scanf("%hd", &asks)==1){
Tree *root=new Tree;
char temp[21];
int index, num=0;
G;
++asks;
while(--asks){
charget(temp);
/*
for(int i=0; temp[i]; ++i) cout << (int)temp[i] << "\t";
cout << endl;
*/
Tree *p=root;
for(int i=0; temp[i]; ++i){
index=temp[i]-'a';
if(p->branch[index]==NULL) p->branch[index]=new Tree;
p=p->branch[index];
}
if(p->num==0){
p->num=++num;
printf("New! %hd\n", num);
}
else printf("Old! %hd\n", p->num);
}
free(root);
}
return 0;
}
找不到錯誤啊...
請大家幫幫忙