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

Re:前排提示:这道题不能使用字典树做

Posted by pipipapasha at 2023-10-22 12:16:32 on Problem 2418
In Reply To:前排提示:这道题不能使用字典树做 Posted by:kkksc03 at 2017-02-06 16:33:24
字典树代码,速度慢了点,7600ms
#include <iostream>
#include <stdio.h>
#include <string.h>
#define XX 100
using namespace std;
class leaf{
public:
    int cnt;
    leaf *p[XX];
    leaf(){
        cnt = 0;
        memset(p,0,sizeof(p));
    }
};
char ret[30],a;
int retcnt=0,all=0;
leaf *root,*L;
void DFS(leaf *L)
{
    if(L!=root&&L->cnt)
    {
        for(int i=0; i<retcnt; i++)cout<<ret[i];
        cout<<' ';
        printf("%.4f\n",(double)L->cnt/all*100);
    }
    for(int i=0;i<XX;i++){
        if(L->p[i]==0)continue;
        ret[retcnt] = ' '+i;
        retcnt++;
        DFS(L->p[i]);
        retcnt--;
    }
}
int main()
{
    L = root = new leaf();
    string s;
    while(getline(cin,s)){
        if(s=="")continue;
        string::iterator start = s.begin();
        while(start<s.end()){
            a = *start;
            start++;
            if(L->p[a-' ']!=0)L = L->p[a-' '];
            else L = L->p[a-' '] = new leaf();
        }
        L->cnt++;
        if(L!=root) all++;
        L=root;
    }
    DFS(root);
    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