zoukankan      html  css  js  c++  java
  • How to use union.

    Union is a datatype in c/c++, but rarely used in practice, because I almost think the memory is enough now, don't need such trick. Recently I have read the source code of NFA , using union very beautiful.
     
    Wen construct a NFA , we have a pushdown stack , record the NFA fragment had been constructed.
    When the construct is not completed, we have to store all the fragments, but how to store them? It is easy to see, we only need to store the start state and the out states of the fragment.
    We may define the fragment as:
    struct Frag
    {
         state   *start;
         Ptrlist  *out;
    };
     
    It's easy to see , Ptrlist is a linklist of state.ok , we define Ptrlist as:
    struct Ptrlist
    {
         state *s;
         Ptrlist *next;
    };
    But to NFA , when the fragment not link to others , the out edge is not sure link to where,  and when linked to others we don't need this out state linklist.so we can use union.
    union xy
    {
         int a;
         int b;
         int c;
    };
    the both a,b,c shared the memory. We can define Ptrlist as
    union Ptrlist
    {
         state *s;
         Ptrlist *next;
    };
    At beginning using *next as a linklist , and then when link out to others using *s.
    by 1957
  • 相关阅读:
    hdu多校4
    hdu多校第三场
    牛客多校4
    bzoj 1477 扩展欧几里德
    bzoj 1485 卡特兰数 + 分解因子
    hdu多校 2
    牛客网暑期多校2
    bzoj 1040 基向内环树dp
    hdu 多校第一场
    SPOJ
  • 原文地址:https://www.cnblogs.com/x1957/p/2866008.html
Copyright © 2011-2022 走看看