『POJ 1451』T9

题目链接:http://poj.org/problem?id=1451
转载声明:http://blog.youkuaiyun.com/sg_siqing/article/details/12207153

个人感想:感觉自己好弱鸡啊…从来没接触过这种变形了…为什么别人的脑洞就是那么大开.自己脑洞就特别的憋屈…看转载吧…不多说…我静静的去画个圈.真够蛋疼了…

分析:字典树变形.
代码:

/* Author:GavinjouElephant
 * Title:
 * Number:
 * main meanning:
 *
 *
 *
 */

//#define OUT
#include <iostream>
using namespace std;
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <cctype>
#include <vector>
#include <set>
#include <cstdlib>
#include <map>
#include <queue>
//#include<initializer_list>
//#include <windows.h>
//#include <fstream>
//#include <conio.h>
#define MaxN 0x7fffffff
#define MinN -0x7fffffff
#define Clear(x) memset(x,0,sizeof(x))
const int INF=0x3f3f3f3f;
const int maxn=1005;
int N,M;
struct Node
{
  string str;
  int Time;
  int id;
};
Node node[maxn];

int atod[26] = {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};
struct wNode
{
    int p;
    wNode* next[26];
};
struct pNode
{
    int p;
    int w;
    pNode* next[10];
};
wNode* wRoot;
pNode* pRoot;

wNode* Build_wNode()
{
    wNode* ans=new wNode();
    ans->p=0;
    for(int j=0;j<26;j++)
    {
        ans->next[j]=NULL;
    }
    return ans;
}
pNode* Build_pNode()
{
    pNode* ans=new pNode();
    ans->p=0;
    ans->w=0;
    for(int j=0;j<10;j++)
    {
        ans->next[j]=NULL;
    }
    return ans;
}

void Delete_wNode(wNode * root)
{
    for(int j=0;j<26;j++)
    {
        if(root->next[j])
        {
            Delete_wNode(root->next[j]);
        }
    }
    delete root;
}

void Delete_pNode(pNode * root)
{
    for(int j=0;j<10;j++)
    {
        if(root->next[j])
        {
            Delete_pNode(root->next[j]);
        }
    }
    delete root;
}
void Create_Node(wNode* wroot,pNode* proot,Node org)
{
    int Len=org.str.length();
    for(int j=0;j<Len;j++)
    {
        int pos=org.str[j]-'a';
        //cout<<pos<<endl;
        if(wroot->next[pos]==NULL)
        {
            wroot->next[pos]=Build_wNode();
        }

        if(proot->next[atod[pos]]==NULL)
        {
            proot->next[atod[pos]]=Build_pNode();
        }

        wroot=wroot->next[pos];
        wroot->p+=org.Time;
        proot=proot->next[atod[pos]];
        if(wroot->p>proot->p)
        {
            proot->p=wroot->p;
            proot->w=org.id;
        }
    }
}
int main()
{
#ifdef OUT
    freopen("coco.txt","r",stdin);
    freopen("lala.txt","w",stdout);
#endif
     int T;
     scanf("%d",&T);
     for(int x=1;x<=T;x++)
     {
        wRoot=Build_wNode();
        pRoot=Build_pNode();
        scanf("%d",&N);
        for(int i=0;i<N;i++)
        {
            cin>>node[i].str>>node[i].Time;
            node[i].id=i;
           // cout<<node[i].str<<" "<<node[i].Time<<endl;
           Create_Node(wRoot,pRoot,node[i]);
        }
        cin>>M;
        printf("Scenario #%d:\n", x);
        string str;
        while(M--)
        {
            cin>>str;
           // cout<<str<<endl;
            pNode* tmp=pRoot;
            int Len=str.length();
            for(int j=0;j<Len-1;j++)
            {
                int x=str[j]-'0';
                if(tmp)
                {
                    tmp=tmp->next[x];
                    if(tmp)
                    {
                       int id=tmp->w;
                       for(int k=0;k<=j;k++)
                       {
                         cout<<node[id].str[k];
                       }
                       printf("\n");
                    }
                    else
                    {
                        printf("MANUALLY\n");
                    }

                }
                else
                {
                    printf("MANUALLY\n");
                }

            }
            printf("\n");
        }
        printf("\n");
        Delete_pNode(pRoot);
        Delete_wNode(wRoot);
     }


    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值