图书列表

题目描述

这里写图片描述

模拟

这种题瞎模拟
我是建了个trie,对于图书和目录分开来存储。一个字符串用它在哈希表的位置作为数值。
然后输出的时候,把一个节点的儿子按字典序排序

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
const int maxn=30*100*2+10,maxh=10000;
map<int,int> g[maxn],f[maxn];
int has[maxh+10],b[maxn];
pi a[maxn];
char ha[maxh+10][110],len[maxh+10],s[110],h[110];
int i,j,k,l,t,n,m,tot,top,cnt,root;
char ch,ch2;
int getid(char ch){
    if (ch>='A'&&ch<='Z') return ch-'A'+1;
    else if (ch>='0'&&ch<='9') return ch-'0'+27;
    else return 37;
}
bool cmp1(int x){
    int i;
    if (top!=len[x]) return 0;
    fo(i,1,top)
        if (h[i]!=ha[x][i]) return 0;
    return 1;
}
int hash(){//get a string hash
    int i,j,k=0;
    fo(i,1,top)
        k=((ll)k*38%maxh+(ll)getid(h[i]))%maxh;
    if (!k) k=maxh;
    j=k;
    while (has[k]!=0&&(has[k]!=j||has[k]==j&&!cmp1(k))) k=k%maxh+1;
    if (!has[k]){
        has[k]=j;
        fo(i,1,top) ha[k][i]=h[i];
        len[k]=top;
    }
    return k;
}
void insert(int &x,int y){
    if (!x) x=++tot;
    int i,j,k;
    fo(i,y,m)   
        if (i==m||s[i]=='/') break;
    top=0;
    fo(j,y,i) h[++top]=s[j];
    if (h[top]=='/') top--;
    k=hash();
    if (i==m) f[x][k]=1;
    else insert(g[x][k],i+1);
}
char get(){
    char ch2=getchar();
    while ((ch2<'0'||ch2>'9')&&(ch2<'A'||ch2>'Z')&&ch2!=' '&&ch2!='/') ch2=getchar();
    return ch2;
}
bool cmp3(int x,int y){
    int i;
    /*if (len[x]<len[y]) return 1;
    if (len[x]>len[y]) return 0;*/
    fo(i,1,min(len[x],len[y]))
        if (ha[x][i]<ha[y][i]) return 1;
        else if (ha[x][i]>ha[y][i]) return 0;
    if (len[x]<=len[y]) return 1;else return 0;
}
bool cmp2(pi a,pi b){
    return cmp3(a.first,b.first);
}
bool cmp4(int x,int y){
    return cmp3(x,y);
}
void write(int x,int y){
    int i;
    fo(i,1,4*(y-1)) printf(" ");
    fo(i,1,len[x]) printf("%c",ha[x][i]);
    printf("\n");
}
void dfs(int x,int y){
    int i,j,k,l,r;
    if (!g[x].empty()){
        l=top+1;
        while (!g[x].empty()){
            a[++top]=(*(g[x].begin()));
            g[x].erase(g[x].begin());
        }
        r=top;
        sort(a+l,a+r+1,cmp2);
        fo(i,l,r){
            write(a[i].first,y);
            dfs(a[i].second,y+1);
        }
    }
    if (!f[x].empty()){
        cnt=0;
        while (!f[x].empty()){
            b[++cnt]=(*(f[x].begin())).first;
            f[x].erase(f[x].begin());
        }
        sort(b+1,b+cnt+1,cmp4);
        fo(i,1,cnt) write(b[i],y);
    }
}
int main(){
    freopen("booklist.in","r",stdin);freopen("booklist.out","w",stdout);
    root=tot=1;
    while (1){
        m=0;
        ch=get();
        ch2=getchar();
        if (ch=='0'&&(ch2<'0'||ch2>'9')&&(ch2<'A'||ch2>'Z')&&ch2!=' '&&ch2!='/') break;
        s[m=1]=ch;
        s[m=2]=ch2;
        while (1){
            ch2=getchar();
            if ((ch2<'0'||ch2>'9')&&(ch2<'A'||ch2>'Z')&&ch2!=' '&&ch2!='/') break;
            s[++m]=ch2;
        }
        insert(root,1);
    }
    dfs(root,1);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值