c_cpp C ++模板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C ++模板相关的知识,希望对你有一定的参考价值。

template<int MAX> class MemoryManager {
	int top, stack[MAX];
	
public:
	void setup() {
		top = MAX;
		for (int i = 0; i < MAX; ++i)
			stack[i] = MAX - i;
	}
	
	int apply() {
		return stack[--top];
	}
	
	void recover(const int &x) {
		stack[top++] = x;
	}
};

class Treap {
	int root;
	
	struct Node {
		int l, r, rand;
		int x, y;
	} tr[maxn];
	
	MemoryManager<maxn> *mm;
	
	void lrotate(int &x) {
		int y = tr[x].l; tr[x].l = tr[y].r; tr[y].r = x;
		x = y;
	}
	
	void rrotate(int &x) {
		int y = tr[x].r; tr[x].r = tr[y].l; tr[y].l = x;
		x = y;
	}
	
	void insert(int &x, const int &_x, const int &_y) {
		if (x == 0) {
			x = mm->apply();
			tr[x].l = tr[x].r = 0;
			tr[x].rand = random(INT_MAX);
			tr[x].x = _x, tr[x].y = _y;
			return;
		}
		if (_x < tr[x].x) {
			insert(tr[x].l, _x, _y);
			if (tr[tr[x].l].rand > tr[x].rand) lrotate(x);
		} else {
			insert(tr[x].r, _x, _y);
			if (tr[tr[x].r].rand > tr[x].rand) rrotate(x);
		}
	}
	
	void discard(int &x, const int &_x) {
		if (x == 0) printf("Wrong in Line %d\n", __LINE__); // ******************************************************
		if (_x == tr[x].x) {
			if (tr[x].l == 0 && tr[x].r == 0) {
				mm->recover(x);
				x = 0;
				return;
			}
			if (tr[tr[x].l].rand > tr[tr[x].r].rand) {
				lrotate(x);
				discard(tr[x].r, _x);
			} else {
				rrotate(x);
				discard(tr[x].l, _x);
			}
			return;
		}
		if (_x < tr[x].x) discard(tr[x].l, _x); else discard(tr[x].r, _x);
	}
	
	int findprev(const int &_x) {
		int ret = -1, x = root;
		while (x) {
			if (tr[x].x < _x) {
				ret = x;
				x = tr[x].r;
			} else
				x = tr[x].l;
		}
		return ret;
	}
	
	int findnext(const int &_x) {
		int ret = -1, x = root;
		while (x) {
			if (tr[x].x > _x) {
				ret = x;
				x = tr[x].l;
			} else
				x = tr[x].r;
		}
		return ret;
	}
	
public:
	long long area;
	
	void setup() {
		randomize;
		root = 0;
		tr[0].l = tr[0].r = 0;
		tr[0].rand = -1;
		mm = new MemoryManager<maxn>();
		mm->setup();
		area = 0;
		insert(root, 0, maxm);
		insert(root, maxm, 0);
	}
	
	void push(const int &_x, const int &_y) {
		int l = findprev(_x), r = findnext(_x - 1);
		if (tr[r].y >= _y) return;
		if (tr[r].x == _x) {
			pop(tr[r].x, tr[r].y);
			r = findnext(_x);
		}
		while (tr[l].y <= _y) {
			pop(tr[l].x, tr[l].y);
			l = findprev(_x);
		}
		area += (long long)(_x - tr[l].x) * (_y - tr[r].y);
		insert(root, _x, _y);
	}
	
	void pop(const int &_x, const int &_y) {
		area -= (long long)(_x - tr[findprev(_x)].x) * (_y - tr[findnext(_x)].y);
		discard(root, _x);
	}
	
	void die() {
		delete mm;
	}
};
#include <cstdio>
#include <cstring>

const int maxn=1000000+10;
const int hp[]={44741, 6020909};
const int hmodnum[]={99999991, 2147483647};

int hpow[2][maxn];
char a[maxn], b[maxn], ra[maxn];
int ha[2][maxn], hb[2][maxn];
int n, ansi=-1, ansj=-1;
int pre[maxn], mc[maxn];

inline bool equal(const int &head, const int &len) {
    if (len==0) return true;
    for (int o=0; o<2; ++o)
        if ((ha[o][head+len-1]-hb[o][len-1]-(long long)ha[o][head-1]*hpow[o][len])%hmodnum[o])
            return false;
    return true;
}

int main() {
    gets(a); gets(b);
    if ((n=strlen(a))!=strlen(b)) {
        printf("-1 -1\n");
        return 0;
    }
    for (int o=0; o<2; ++o) {
        hpow[o][0]=1;
        for (int i=1; i<=n; ++i)
            hpow[o][i]=(long long)hpow[o][i-1]*hp[o]%hmodnum[o];
    }
    
    for (int o=0; o<2; ++o) {
        ha[o][0]=a[0]; hb[o][0]=b[0];
        for (int i=1; i<n; ++i) {
            ha[o][i]=((long long)ha[o][i-1]*hp[o]+a[i])%hmodnum[o];
            hb[o][i]=((long long)hb[o][i-1]*hp[o]+b[i])%hmodnum[o];
        }
    }
    
    for (int i=0; i<n; ++i)
        ra[i]=a[n-1-i];
    
    pre[0]=-1;
    for (int i=1, j=-1; i<n; ++i) {
        while (j>-1 && ra[i]!=ra[j+1]) j=pre[j];
        if (ra[i]==ra[j+1]) ++j;
        pre[i]=j;
    }
    mc[0]=(b[0]==ra[0])?0:-1;
    for (int i=1, j=mc[0]; i<n; ++i) {
        while (j>-1 && b[i]!=ra[j+1]) j=pre[j];
        if (b[i]==ra[j+1]) ++j;
        mc[i]=j;
    }
    
    for (int i=0, j; i+1<n && a[i]==b[n-1-i]; ++i)
        if (mc[j=n-1-i-1]>-1)
            if (equal(i+1, n-1-mc[j]-i-1))
                ansi=i, ansj=n-1-mc[j];
    printf("%d %d\n", ansi, ansj);
    
    return 0;
}
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;

const int maxl=100+5;
const int modnum=100000;

typedef int Tmatrix[maxl][maxl];

int sign[300];
int n, tot;
int trie[maxl][4], pre[maxl];
bool seg[maxl], vis[maxl];
char ch[20];
queue<int> q;
Tmatrix g, f, T;

void mult(Tmatrix &c, Tmatrix &a, Tmatrix &b)
{
    for (int i=0; i<=tot; ++i)
        for (int j=0; j<=tot; ++j)
        {
            long long tmp=0;
            for (int k=0; k<=tot; ++k)
                tmp+=(long long)a[i][k]*b[k][j];
            c[i][j]=tmp%modnum;
        }
    memcpy(a, c, sizeof(a));
}

void prework()
{
    sign['A']=0, sign['C']=1, sign['G']=2, sign['T']=3;
}

void init()
{
    int m;
    scanf("%d%d", &m, &n);
    tot=0;
    memset(trie, 0, sizeof(trie));
    memset(seg, 0, sizeof(seg));
    for (int i=0; i<m; ++i)
    {
        scanf("%s", ch);
        int len=strlen(ch);
        int k=0;
        for (int j=0; j<len; ++j)
        {
            int p=sign[ch[j]];
            if (trie[k][p]==0) trie[k][p]=++tot;
            k=trie[k][p];
        }
        seg[k]=true;
    }
    
    pre[0]=0;
    memset(vis, 0, sizeof(vis));
    vis[0]=true;
    q.push(0);
    while (!q.empty())
    {
        int x=q.front(), y; q.pop();
        for (int i=0; i<4; ++i)
            if (!vis[y=trie[x][i]])
            {
                q.push(y); vis[y]=true;
                int j=pre[x];
                while (j>0 && trie[j][i]==0) j=pre[j];
                pre[y]=(x==0)?0:trie[j][i];
                if (seg[pre[y]]) seg[y]=true;
                for (int k=0; k<4; ++k)
                    if (trie[y][k]==0)
                        trie[y][k]=trie[pre[y]][k];
            }
    }
}

void solve()
{
    memset(g, 0, sizeof(g));
    for (int i=0; i<=tot; ++i)
        if (!seg[i])
            for (int j=0; j<4; ++j)
                if (!seg[trie[i][j]])
                    ++g[i][trie[i][j]];
    memcpy(f, g, sizeof(g));--n;
    
    while (n>0)
    {
        if (n&1) mult(T, f, g);
        mult(T, g, g);
        n>>=1;
    }
    
    long long ans=0;
    for (int i=0; i<=tot; ++i)
        if (!seg[i])
            ans+=f[0][i];
    ans%=modnum;
    printf("%I64d\n", ans);
}

int main()
{
    prework();
    init();
    solve();
    system("pause");
    return 0;
}
#include <cstdio>
#include <cstring>

#define ms(x, y) memset(x, y, sizeof(x))

const int maxn=20000+10;
const int maxm=50000+10;

int n, m, group;
int e_l[maxn], e_s[maxm], e_t[maxm], e_next[maxm];
int belong[maxn], dfn[maxn], low[maxn];
int tot, top, stack[maxn];
bool in_stack[maxn];
int in_degree[maxn], out_degree[maxn], IN, OUT;

void dfs(int x) {
	dfn[x]=low[x]=++tot;
	in_stack[stack[++top]=x]=true;
	int y;
	for (int i=e_l[x]; i>0; i=e_next[i])
		if (dfn[y=e_t[i]]==0) {
			dfs(y);
			if (low[y]<low[x]) low[x]=low[y];
		} else {
			if (in_stack[y] && dfn[y]<low[x])
				low[x]=dfn[y];
		}
	if (dfn[x]==low[x]) {
		++group;
		do {
			in_stack[y=stack[top--]]=false;
			belong[y]=group;
		} while (y!=x);
	}
}

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d%d", &n, &m);
		ms(e_l, 0);
		for (int i=1; i<=m; ++i) {
			scanf("%d%d", &e_s[i], &e_t[i]);
			e_next[i]=e_l[e_s[i]], e_l[e_s[i]]=i;
		}
		
		ms(dfn, 0); ms(in_stack, 0);
		group=tot=top=0;
		for (int i=1; i<=n; ++i)
			if (dfn[i]==0)
				dfs(i);
		if (group==1) {
			puts("0");
			continue;
		}
		
		ms(in_degree, 0), ms(out_degree, 0);
		for (int i=1; i<=m; ++i)
			if (belong[e_s[i]]!=belong[e_t[i]])
				++in_degree[belong[e_t[i]]], ++out_degree[belong[e_s[i]]];
		
		IN=OUT=0;
		for (int i=1; i<=group; ++i) {
			if (in_degree[i]==0) ++IN;
			if (out_degree[i]==0) ++OUT;
		}
		printf("%d\n", IN>OUT?IN:OUT);
	}
	
	return 0;
}
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;

#define randomize srand((unsigned)time(NULL))
#define random(x) ((rand() * (rand() + rand())) % (x))
#define ms(x, y) memset(x, y, sizeof(x))
#define mc(x, y) memcpy(x, y, sizeof(x))

int T;
int ans;

int main() {
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    cin >> T;
    for (int cs = 1; cs <= T; ++cs) {
        cout << "Case #" << cs << ": " << ans << endl;
    }

    return 0;
}

以上是关于c_cpp C ++模板的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 基本C ++模板

c_cpp 基本C ++模板

c_cpp c ++基本模板

c_cpp C ++标准模板库

c_cpp CodeForces C ++模板

c_cpp c ++的简单模板