Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

真不知道为什么会一直WA……

Posted by syngod at 2008-12-04 20:01:56 on Problem 1119
看了题目想都没想就想用字典树……
然后敲了以下代码……
WA了一天……
题目给的Sample过了,自己想的样例也过了
可就是一直WA……
T.T
哪位大牛来帮帮忙……


代码如下 :


#include <iostream>
using namespace std;
#include <string.h>
#include <math.h>

static double score=0.0;

char datatmp[5000],data[5000];

typedef struct song
{
        int count;
        int tmpcount;
        struct song *next[35];
} trie;

bool ckvalid(char b)
{
     if((b>='a'&&b<='z')||(b>='0'&&b<='9')) return true;
     return false;
}

int mp(char g)
{
    if(g<='z'&&g>='a') return g-'a';
    else return 26+g-'0';
    
}

void filter()
{
     int p=0;
     int pp=0;
     while(datatmp[p]!='\0')
     {
         if(datatmp[p]>='A'&&datatmp[p]<='Z') data[pp++]=datatmp[p++]+32;
         else if(ckvalid(datatmp[p])||datatmp[p]==' ') 
         {
              data[pp++]=datatmp[p];
              p++;
         }
         else p++;
         
     }
     data[pp]='\0';

     
}

void readln()
{
     int p=0;
     char t=getchar();
     while(t!='\n'&&t!=EOF)
     {
         datatmp[p++]=t;
         t=getchar();
     }
     datatmp[p]='\0';
     filter();
}



trie* datainsert(trie *root,char *wd)
{
     
      trie *tm=root;
     if(tm==NULL)
     {
        
         tm=new trie;
         int i;
         tm->count=0;
         for(i=0;i<36;i++) tm->next[i]=NULL;
     }
     if((*wd)=='\0') 
     {
                   tm->count++;
                   return tm;
     }
     
     int mpp=mp(*wd);
     

     
     tm->next[mpp]=datainsert(tm->next[mpp],wd+1);
     
     
     
     
     return tm;
}



void inittrie(trie *root)
{
     if(!root) return;
     root->tmpcount=0;
     int i;
     for(i=0;i<36;i++) inittrie(root->next[i]);
     return;
     
}

void counttrie(trie *root)
{
     score+=sqrt(((long int)(root->count))*root->tmpcount);
     int i;
     for(i=0;i<36;i++) if(root->next[i]) counttrie(root->next[i]);
     return;
     
     
}

void datacountadd(trie *root,char *wd)
{
    int p=0;
    trie *pt=root;
    while(wd[p]!='\0'&&pt)
    {
        pt=pt->next[mp(wd[p])];
        p++;
        
    }
    
    
    
    
    if(pt)
    {
           pt->tmpcount++;
    }
    
}



int main()
{
    trie *hd=new trie;
    int h;
    hd->count=0;
    for(h=0;h<36;h++)  hd->next[h]=NULL;
    
    
    readln();
    char tmpwd[5000];

    char t;
    int p=0;
    int pw=0;
    

    while(1)
    {
        if(data[p]!=' '&&data[p]!='\0') tmpwd[pw++]=data[p];
        else
        {
            
            if(pw!=0)
            {
                     tmpwd[pw]='\0';
                     pw=0;
                     
                     hd=datainsert(hd,tmpwd);
            }
            
            
        }
        
        
        if(data[p]=='\0')   break;              
        p++;   
    }
    
    
    
    
    readln();
    bool f=true;

    while(f)
    {
        inittrie(hd);
        int ttllines=0;
        

        score=0.0;
        
        
        while(f)
        {
                readln();

                
                if(!strcmp(datatmp,"----------")) 
                {
                                               if(ttllines==0) f=false;
                                               break;
                }
                ttllines++;
                p=0;
                pw=0;
              while(f)
               {
               
            
               if(data[p]!=' '&&data[p]!='\0') tmpwd[pw++]=data[p];
               else
               {
                   if(pw!=0)
                   {
                                   tmpwd[pw]='\0';
                                    pw=0;
                                    datacountadd(hd,tmpwd);
                   }
            
            
                }
        
        
                                    if(data[p]=='\0')   break;              
                                     p++;   
                                      }
           
        }                                     
           
    
        counttrie(hd);
        printf("%.2f\n",score);     
           
        
       
        
    }
    

    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    return 0;
}



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator