ACM算法竞赛的一些东西 头文件

sort
stable_sort()
is_sorted()
strtok
string.c_str();

char s[MXN], *token;
token = strtok(s, "\\");
//继续获取其他的子字符串
while( token != NULL ) {
    printf( "%s\n", token );
    token = strtok(NULL, "\\");
}

/*
 *  空格作为分隔输入,读取一行的整数
 */
gets(buf);

int v;
char *p = strtok(but, " "); 
while (p){
    sscanf(p, "%d", &v);
    p = strtok(NULL," "); 
}
hash_map

好用

struct Hash_map{
    static const int mask=0x7fffff;
    LL p[mask+1],q[mask+1];
    void clear(){
        memset(q,0,sizeof(q));
    }
    LL& operator [](LL k){
        LL i;
        for(i=k&mask;q[i]&&p[i]!=k;i=(i+1)&mask);
        p[i]=k;
        return q[i];
    }
}mp;
struct Hash_table {
    static const int V=1000003;
    int fst[V],nxt[V];
    int ctm,ptm[V],T;
    int val[V];
    vector<pair<ll,int>> key;
    void init() { T=0; ctm++; key.clear(); }
    void add(ll s) {
        int S=s%V;
        if (ptm[S]!=ctm) ptm[S]=ctm,fst[S]=-1;
        for (int j=fst[S];j!=-1;j=nxt[j]) if (key[j].fi==s) {
            key[j].se++;
            return;
        }
        nxt[T]=fst[S],fst[S]=T; key.pb(mp(s,1));
        T++;
    }
    int operator [](ll s) {
        int S=s%V;
        if (ptm[S]!=ctm) return 0;
        for (int j=fst[S];j!=-1;j=nxt[j]) if (key[j].fi==s)
            return key[j].se;
        return 0;
    }
}cnt;
常用头文件:

g++ -std=c++14 A.cpp -o A -D LH_LOCAL

/*
**链接**
传送门: [here]()
**题意**
**思路**
**备注**
*/
#pragma comment(linker, "/STACK:102400000,102400000")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize("Ofast,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
#define fi first
#define se second
#define o2(x) (x) * (x)
#define mk make_pair
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset((a), (b), sizeof((a)))
#define rep(i, s, t) for(register int i = (s), LIM=(t); i < LIM; ++i)
#define per(i, s, t) for(register int i = (s), LIM=(t); i >= LIM; --i)
#define GKD std::ios::sync_with_stdio(false);cin.tie(0)
#define my_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long LL;
typedef long long int64;
typedef unsigned long long uint64;
typedef pair<int, int> pii;
// mt19937 rng(time(NULL));//std::clock()
// mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());
// shuffle(arr, arr + n, rng64);
inline int64 read() {
    int64 x = 0;int f = 0;char ch = getchar();
    while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch =
    getchar(); return x = f ? -x : x;
}
inline void write(int64 x, bool f = true) {
    if (x == 0) {putchar('0'); if(f)putchar('\n');else putchar(' ');return;}
    if (x < 0) {putchar('-');x = -x;}
    static char s[23];
    int l = 0;
    while (x != 0)s[l++] = x % 10 + 48, x /= 10;
    while (l)putchar(s[--l]);
    if(f)putchar('\n');else putchar(' ');
}
int lowbit(int x) { return x & (-x); }
template <class T>
T big(const T &a1, const T &a2) {return a1 > a2 ? a1 : a2;}
template <class T>
T sml(const T &a1, const T &a2) {return a1 < a2 ? a1 : a2;}
template <typename T, typename... R>
T big(const T &f, const R &... r) {return big(f, big(r...));}
template <typename T, typename... R>
T sml(const T &f, const R &... r) {return sml(f, sml(r...));}
void debug_out() { cout << '\n'; }
template <typename T, typename... R>
void debug_out(const T &f, const R &... r) {
    cout << f << " ";
    debug_out(r...);
}
#ifdef LH_LOCAL
#define debug(...) cout << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__);
#else
#define debug(...) ;
#endif
/*================Header Template==============*/
const int mod = 998244353;// 998244353
const int INF = 0x3f3f3f3f;
int ksm(int a, int64 b, int kmod = mod) {int res = 1;for(;b > 0;b >>= 1, a = (int64)a * a % kmod) if(b &1) res = (int64)res * a % kmod;return res;}
const int MXN = 2e5 + 5;

int n, m;
void work() {
    n = read();
}
int main() {
#ifdef LH_LOCAL
    freopen("D:/ACM/mtxt/in.txt", "r", stdin);
    // freopen("D:/ACM/mtxt/out.txt", "w", stdout);
#endif
    for(int cas = 1, tim = 1; cas <= tim; ++ cas) {
        // printf("Case #%d:\n", cas);
        work();
    }
#ifdef LH_LOCAL
    cout << "time cost:" << 1.0 * clock() / CLOCKS_PER_SEC << "s" << endl;
#endif
    return 0;
}

vscode snippets

{
	// Place your snippets for cpp here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	// "Print to console": {
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// },
	"简单线段树模板": {
		"prefix": "mb_seg1",
		"body": [
			"class SegT1 {",
"    public:",
"#define lson rt << 1",
"#define rson rt << 1 | 1",
"    int N;",
"    vector<int> lazy;",
"    vector<int64_t> sum;",
"    SegT1(int N = 0) : N(N), lazy((N << 2) + 5), sum((N << 2) + 5) {}",
"    void pop(int rt) {",
"        sum[rt] = (sum[lson] + sum[rson]);",
"    }",
"    void build(int l, int r, int rt) {",
"        lazy[rt] = 0;",
"        if(l == r) {",
"            sum[rt] = 1;",
"            return ;",
"        }",
"        int mid = (l + r) >> 1;",
"        build(l, mid, lson), build(mid + 1, r, rson);",
"        pop(rt);",
"    }",
"    void push(int rt, int l, int r) {",
"        if(lazy[rt] == 0) return ;",
"        int mid = (l + r) >> 1;",
"        lazy[lson] += lazy[rt];",
"        lazy[rson] += lazy[rt];",
"        sum[lson] += lazy[rt] * (mid - l + 1);",
"        sum[rson] += lazy[rt] * (r - mid);",
"        lazy[rt] = 0;",
"    }",
"    void apply(int rt, int l, int r, int v) {",
"        lazy[rt] += v;",
"        sum[rt] += v * (r - l + 1);",
"    }",
"    void update(int v, int L, int R, int l, int r, int rt) {",
"        if(L <= l && r <= R) {",
"            apply(rt, l, r, v);",
"            return ;",
"        }",
"        int mid = (l + r) >> 1;",
"        push(rt, l, r);",
"        if(L > mid) {update(v, L, R, mid + 1, r, rson);}",
"        else if(R <= mid) {update(v, L, R, l, mid, lson);}",
"        else {",
"            update(v, L, mid, l, mid, lson);",
"            update(v, mid + 1, R, mid + 1, r, rson);",
"        }",
"        pop(rt);",
"    }",
"    int query(int L, int R, int l, int r, int rt) {",
"        if(L <= l && r <= R) {",
"            return sum[rt];",
"        }",
"        int mid = (l + r) >> 1, ans = 0;",
"        push(rt, l, r);",
"        if(L > mid) {ans += query(L, R, mid + 1, r, rson);}",
"        else if(R <= mid) {ans += query(L, R, l, mid, lson);}",
"        else {",
"            ans += query(L, mid, l, mid, lson);",
"            ans + query(mid + 1, R, mid + 1, r, rson);",
"        }",
"        return ans;",
"        pop(rt);",
"    }",
"#undef lson",
"#undef rson",
"};",
		],
		"description": "简单线段树模板"
	},
	"freopen模板": {
		"prefix": "mb_freopen",
		"body": [
			"#ifdef LH_LOCAL",
"    freopen(\"D:/ACM/mtxt/in.txt\", \"r\", stdin);",
"    // freopen(\"D:/ACM/mtxt/out.txt\", \"w\", stdout);",
"#endif",
		],
		"description": "freopen模板"
	},
	"头文件完整": {
		"prefix": "mb_headfile1",
		"body": [
			"#include <bits/stdc++.h>",
"#define fi first",
"#define se second",
"#define o2(x) (x) * (x)",
"#define mk make_pair",
"#define eb emplace_back",
"#define SZ(x) ((int)(x).size())",
"#define all(x) (x).begin(), (x).end()",
"#define clr(a, b) memset((a), (b), sizeof((a)))",
"#define rep(i, s, t) for(register int i = (s), LIM=(t); i < LIM; ++i)",
"#define per(i, s, t) for(register int i = (s), LIM=(t); i >= LIM; --i)",
"#define GKD std::ios::sync_with_stdio(false);cin.tie(0)",
"#define my_unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())",
"using namespace std;",
"typedef long long LL;",
"typedef long long int64;",
"typedef unsigned long long uint64;",
"typedef pair<int, int> pii;",
"// mt19937 rng(time(NULL));//std::clock()",
"// mt19937_64 rng64(chrono::steady_clock::now().time_since_epoch().count());",
"// shuffle(arr, arr + n, rng64);",
"inline int64 read() {",
"    int64 x = 0;int f = 0;char ch = getchar();",
"    while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();",
"    while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + ch - '0', ch =",
"    getchar(); return x = f ? -x : x;",
"}",
"inline void write(int64 x, bool f = true) {",
"    if (x == 0) {putchar('0'); if(f)putchar('\\n');else putchar(' ');return;}",
"    if (x < 0) {putchar('-');x = -x;}",
"    static char s[23];",
"    int l = 0;",
"    while (x != 0)s[l++] = x % 10 + 48, x /= 10;",
"    while (l)putchar(s[--l]);",
"    if(f)putchar('\\n');else putchar(' ');",
"}",
"int lowbit(int x) { return x & (-x); }",
"template <class T>",
"T big(const T &a1, const T &a2) {return a1 > a2 ? a1 : a2;}",
"template <class T>",
"T sml(const T &a1, const T &a2) {return a1 < a2 ? a1 : a2;}",
"template <typename T, typename... R>",
"T big(const T &f, const R &... r) {return big(f, big(r...));}",
"template <typename T, typename... R>",
"T sml(const T &f, const R &... r) {return sml(f, sml(r...));}",
"void debug_out() { cout << '\\n'; }",
"template <typename T, typename... R>",
"void debug_out(const T &f, const R &... r) {",
"    cout << f << \" \";",
"    debug_out(r...);",
"}",
"#ifdef LH_LOCAL",
"#define debug(...) cout << \"[\" << #__VA_ARGS__ << \"]: \", debug_out(__VA_ARGS__);",
"#else",
"#define debug(...) ;",
"#endif",
"/*================Header Template==============*/",
"const int mod = 998244353;// 998244353",
"const int INF = 0x3f3f3f3f;",
"int ksm(int a, int64 b, int kmod = mod) {int res = 1;for(;b > 0;b >>= 1, a = (int64)a * a % kmod) if(b &1) res = (int64)res * a % kmod;return res;}",
"const int MXN = 2e5 + 5;",
"",
"int n, m;",
"void work() {",
"    n = read();",
"}",
"int main() {",
"#ifdef LH_LOCAL",
"    freopen(\"D:/ACM/mtxt/in.txt\", \"r\", stdin);",
"    // freopen(\"D:/ACM/mtxt/out.txt\", \"w\", stdout);",
"#endif",
"    for(int cas = 1, tim = 1; cas <= tim; ++ cas) {",
"        // printf(\"Case #%d:\\n\", cas);",
"        work();",
"    }",
"#ifdef LH_LOCAL",
"    cout << \"time cost:\" << 1.0 * clock() / CLOCKS_PER_SEC << \"s\" << endl;",
"#endif",
"    return 0;",
"}",
		],
		"description": "头文件完整"
	},
	"头文件部分": {
		"prefix": "mb_headfile2",
		"body": [
			"#include <bits/stdc++.h>",
"using namespace std;",
"void debug_out() { cout << '\\n'; }",
"template <typename T, typename... R>",
"void debug_out(const T &f, const R &... r) {",
"    cout << f << \" \";",
"    debug_out(r...);",
"}",
"#ifndef ONLINE_JUDGE",
"#define debug(...) cout << \"[\" << #__VA_ARGS__ << \"]: \", debug_out(__VA_ARGS__);",
"#else",
"#define debug(...) ;",
"#endif",
"typedef long long LL;",
"bool Finish_read;",
"inline long long Rd() {long long x = 0;Finish_read = 0;int f = 1;char ch = getchar();while (!isdigit(ch)) {if (ch == '-') f = -1;if (ch == EOF) return x;ch = getchar();}while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();x *= f;Finish_read = 1;return x;}",
"template <class T>inline void print(T x) {if(x > 9) print(x / 10), putchar(x % 10 ^ 48);else if(x < 0) putchar('-'), print(-x);else putchar(x % 10 ^ 48);}",
"template <class T>inline void write(T x, char c = '\\n') {print(x);putchar(c);}",
"#define read(x) x = Rd()",
"#define read2(x, y) x = Rd(), y = Rd()",
"#define read3(x, y, z) x = Rd(), y = Rd(), z = Rd()",
"",
"constexpr int mod = 998244353;",
"constexpr int INF = 0x3f3f3f3f;",
"constexpr int mxN = 2e5 + 5;",
"int n, m;",
"int ans;",
"void prework() {",
"    read(n);",
"    debug(n)",
"}",
"void work() {",
"    ",
"}",
"void finalwork() {",
"    write(n);",
"}",
"int main() {",
"    for(int Case = 1, allc = 1; Case <= allc; ++ Case) {",
"        prework();",
"        work();",
"        finalwork();",
"    }",
"    return 0;",
"}",
		],
		"description": "头文件部分"
	},
}
读入挂:

更多最快的fread读入挂:传送门

namespace IO {
    const int MX = 4e7; //1e7占用内存11000kb
    char buf[MX]; int c, sz;
    void begin() {
        c = 0;
        sz = fread(buf, 1, MX, stdin);
    }
    inline bool read(int &t) {
        while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++;
        if(c >= sz) return false;
        bool flag = 0; if(buf[c] == '-') flag = 1, c++;
        for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0';
        if(flag) t = -t;
        return true;
    }
}

template<typename T>
inline T read(T&x){
  x=0;int f=0;char ch=getchar();
  while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();
  while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
  return x=f?-x:x;
}
template<typename T>
inline void write(T x){
  if(x==0){ putchar('0'),putchar('\n');return;}
  if(x<0){ putchar('-'),x = -x; }
  static char s[22];int l = 0;
  while(x!=0) s[l++] = x%10+48,x /= 10;
  while(l)putchar(s[--l]);
  putchar('\n');
}

struct fastio{
    char s[100005];
    int it,len;
    fastio(){it=len=0;}
    inline char get(){
        if(it<len)return s[it++];it=0;
        len=fread(s,1,100000,stdin);
        if(len==0)return EOF;else return s[it++];
    }
    bool notend(){
        char c=get();
        while(c==' '||c=='\n')c=get();
        if(it>0)it--;
        return c!=EOF;
    }
}BUFF;
#define read(x) x=getnum()
#define write(x) putnum(x),putchar(' ')
#define writeln(x) putnum(x),putchar('\n')

inline LL getnum(){
    LL r=0;bool ng=0;char c;c=BUFF.get();
    while(c!='-'&&(c<'0'||c>'9'))c=BUFF.get();
    if(c=='-')ng=1,c=BUFF.get();
    while(c>='0'&&c<='9')r=r*10+c-'0',c=BUFF.get();
    return ng?-r:r;
}
template<class T> inline void putnum(T x){
    if(x<0)putchar('-'),x=-x;
    register short a[20]={},sz=0;
    while(x)a[sz++]=x%10,x/=10;
    if(sz==0)putchar('0');
    for(int i=sz-1;i>=0;i--)putchar('0'+a[i]);
}
inline char getreal(){char c=BUFF.get();while(c<=32)c=BUFF.get();return c;}
杜教的求解线性递推式的板子
//BM
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef long long LL;
typedef vector<LL> VI;
typedef pair<int,int> PII;
const LL mod=1000000007;
LL powmod(LL a,LL b) {LL res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head

namespace linear_seq {
    const int N = 10010;
    LL res[N],base[N],_c[N],_md[N];

    vector<int> Md;
    void mul(LL *a,LL *b,int k) {
        rep(i,0,k+k) _c[i]=0;
        rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
        for (int i=k+k-1;i>=k;i--) if (_c[i])
            rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
        rep(i,0,k) a[i]=_c[i];
    }
    LL solve(LL n,VI a,VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
//        printf("%d\n",SZ(b));
        LL ans=0,pnt=0;
        int k=SZ(a);
        assert(SZ(a)==SZ(b));
        rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
        Md.clear();
        rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
        rep(i,0,k) res[i]=base[i]=0;
        res[0]=1;
        while ((1LL<<pnt)<=n) pnt++;
        for (int p=pnt;p>=0;p--) {
            mul(res,res,k);
            if ((n>>p)&1) {
                for (int i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
                rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
            }
        }
        rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
        if (ans<0) ans+=mod;
        return ans;
    }
    VI BM(VI s) {
        VI C(1,1),B(1,1);
        LL L=0,m=1,b=1;
        rep(n,0,SZ(s)) {
            LL d=0;
            rep(i,0,L+1) d=(d+(LL)C[i]*s[n-i])%mod;
            if (d==0) ++m;
            else if (2*L<=n) {
                VI T=C;
                LL c=mod-d*powmod(b,mod-2)%mod;
                while (SZ(C)<SZ(B)+m) C.pb(0);
                rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
                L=n+1-L; B=T; b=d; m=1;
            } else {
                LL c=mod-d*powmod(b,mod-2)%mod;
                while (SZ(C)<SZ(B)+m) C.pb(0);
                rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
                ++m;
            }
        }
        return C;
    }
    LL gao(VI a,LL n) {
        VI c=BM(a);
        c.erase(c.begin());
        rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
        return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
    }
};
LL n, m;
int main() {
    scanf("%lld%d", &n, &m);
    vector<LL> dp(501);
    for(int i = 1; i <= m; ++i) dp[i-1] = 1;
    dp[m-1] = 2;
    if(n <= m) {
        printf("%lld\n", dp[n-1]);
        return 0;
    }
    for(int i = m; i <= 500; ++i) {
        dp[i] = (dp[i-1] + dp[i-m]) % mod;
    }
    VI vs;//VI{1,2,4,7,13,24}
    printf("%d\n",linear_seq::gao(dp,n-1));
}

算数表达式转DAG

#include<bits/stdc++.h>
using namespace std;
 
vector<string> suffix_expression;
vector<string> operator_stack;
 
vector<string> opt_table;
map<string, int> opt_id_map;
map<string, int> opt_priority;
 
 
struct node {
    int l, r;
    int opt;
    string label;
    bool operator < (const node a) const {
        return opt <= a.opt;
    }
    bool operator == (const node a) const {
        return l == a.l && r == a.r && opt == a.opt && label == a.label;
    }
};
 
vector<struct node> node_table;  
map<struct node, int> node_id_map;
 
int new_opt(string s, int priority)
{
    opt_table.push_back(s);
    opt_id_map[s] = opt_table.size() - 1;
    opt_priority[s] = priority;
    return opt_table.size() - 1;
}
 
void print_opt(int opt_id)
{
    cout << "opt: " << opt_table[opt_id] << " id: " << opt_id << endl;
}
 
int new_node(struct node nd)
{
    node_table.push_back(nd);
    node_id_map[nd] = node_table.size() - 1;
    return node_table.size() - 1;
}
 
void init()
{
    int opt_id;
    new_opt("#", 0);
    opt_id = new_opt("+", 1);
    print_opt(opt_id);
    opt_id = new_opt("-", 1);
    print_opt(opt_id);
    opt_id = new_opt("*", 2);
    print_opt(opt_id);
    opt_id = new_opt("/", 2);
    print_opt(opt_id);
    opt_id = new_opt("%", 2);
    print_opt(opt_id);
    opt_id = new_opt("!", 3);
    print_opt(opt_id);
    opt_id = new_opt("~", 3);
    print_opt(opt_id);
 
    new_node(node{0, 0, 0});
}
 
void emtpy_opt_stack()
{
    while (operator_stack.back() != "(") {
        suffix_expression.push_back(operator_stack.back());
        operator_stack.pop_back();
    }
    operator_stack.pop_back();
}
 
void generate_suffix_expression()
{
    string s;
    cout<<"please input expression: ";
    cin >> s;
    int last = 0;
    operator_stack.push_back("(");
    for (int i = 0; i < s.length(); ++i) {
        if (opt_id_map[string{s[i]}]) {
            suffix_expression.push_back(s.substr(last, i-last));
            last = i + 1;
            while (operator_stack.back() != "(" && opt_priority[operator_stack.back()] > opt_priority[string{s[i]}]) {
                suffix_expression.push_back(operator_stack.back());
                operator_stack.pop_back();
            }
            operator_stack.push_back(string{s[i]});
        } else if (s[i] == '(') {
            suffix_expression.push_back(s.substr(last, i-last));
            last = i + 1;
            operator_stack.push_back("(");
        } else if (s[i] == ')') {
            suffix_expression.push_back(s.substr(last, i-last));
            last = i + 1;
            emtpy_opt_stack();
        }
    }
    suffix_expression.push_back(s.substr(last, s.length()-1));
    emtpy_opt_stack();
}
 
int get_node_id(struct node nd)
{
    if (node_id_map[nd] > 0) {
        return node_id_map[nd];
    } else {
        return new_node(nd);
    }
}
 
void dfs(int id)
{
    if (id == 0) {
        return;
    }
    cout << "node_id: " << id << " left_node_id: " << node_table[id].l << " right_node_id: " << node_table[id].r\
     << " opt_id: " << node_table[id].opt << " label: " << node_table[id].label << endl;
    dfs(node_table[id].l);
    dfs(node_table[id].r);
}
 
int generate_dag()
{
    vector<int> s;
    for (int i=0; i<suffix_expression.size(); ++i) {
        if (suffix_expression[i] == "") {
            continue;
        } else if (opt_id_map[suffix_expression[i]]) {
            int second = s.back();
            s.pop_back();
            int first = s.back();
            s.pop_back();
            int node_id = get_node_id(node{first, second, opt_id_map[suffix_expression[i]], ""});
            s.push_back(node_id);
        } else {
            int node_id = get_node_id(node{0, 0, 0, suffix_expression[i]});
            s.push_back(node_id);
        }
    }
    return s.back();
}
 
void output_dag(int root_id)
{
    dfs(root_id);
}
 
int main()
{
    init();
    generate_suffix_expression();
    int root_id = generate_dag();
    output_dag(root_id);
} 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值