ACM集训7月5号

常见的数据结构

今日题单链接!!!!!!!!!!

  1. 队列 , 单调队列
  2. 栈 , 单调栈
  3. 并查集
  4. Trie字符串统计
  5. KMP
  6. 哈希
  7. 树状数组 线段树 平衡树 AC自动机 等等

Gingerの神罚【单调栈】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int,int> PII;
#define x first
#define y second
const int N = 2e5 + 10;
int a[N] , n , dp[N] , b[N] , ans = -0x3f3f3f3f;
signed main()
{
    scanf("%lld" , &n);
    for(int i = 1; i <= n ; i ++)  scanf("%lld" , &a[i]);
    stack<PII> s; // 单调栈维护的是 pair , 不会的话百度查一下 
    s.push({0 , 0}); // 栈初始化 :如果没有查找到左边第一个比它小的数 , 等价于 dp[i] = a[i] * (i - 0)  
    for(int i = 1; i <= n ; i ++)
    {
        while(s.size() && a[i] <= s.top().x)  s.pop();
	    PII last = s.top();
        s.push({a[i] , i});
        dp[i] = dp[last.y] + a[i] * (i - last.y);
        ans = max(ans , dp[i]);
    }
    printf("%lld\n" , ans);
}

滑动窗口 /【模板】单调队列

#include <iostream>

using namespace std;

const int N = 1000010;

int a[N], q[N];

int main()
{
    int n, k;
    scanf("%d%d", &n, &k);
    for (int i = 0; i < n; i ++ ) scanf("%d", &a[i]);

    int hh = 0, tt = -1;
    for (int i = 0; i < n; i ++ )
    {
        if (hh <= tt && i - k + 1 > q[hh]) hh ++ ;

        while (hh <= tt && a[q[tt]] >= a[i]) tt -- ;
        q[ ++ tt] = i;

        if (i >= k - 1) printf("%d ", a[q[hh]]);
    }

    puts("");

    hh = 0, tt = -1;
    for (int i = 0; i < n; i ++ )
    {
        if (hh <= tt && i - k + 1 > q[hh]) hh ++ ;

        while (hh <= tt && a[q[tt]] <= a[i]) tt -- ;
        q[ ++ tt] = i;

        if (i >= k - 1) printf("%d ", a[q[hh]]);
    }

    puts("");

    return 0;
}

【模板】并查集

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int p[N] , a[N] , n , m;
int find(int x)
{
	if(x == p[x])  return x;
	else return p[x] = find(p[x]);
}
signed main()
{
    cin >> n >> m;
    for(int i = 1 ; i <= n ; i ++)
    {
    	p[i] = i;
	}
    while(m --)
    {
    	int a , b , c;
    	cin >> c >> a >> b;
    	if(c == 1)
    	{
    		a = find(a) , b = find(b);
    		if(a != b)
    		{
    			p[b] = a;
			}
		}
		else
		{
			if(find(a) != find(b))  cout << "N" << endl;
			else cout << "Y" << endl;
		}
	}
}

P1892 [BOI2003]团伙

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 10;
int p[N] , a[N] , n , m;
int find(int x)
{
	if(x == p[x])  return x;
	else return p[x] = find(p[x]);
}
signed main()
{
    cin >> n >> m;
    for(int i = 1 ; i <= 2 * n ; i ++)
    {
    	p[i] = i;
	}	
	int ct = 0;
    while(m --)
    {
    	int a , b;
    	char c;
    	cin >> c >> a >> b;
    	if(c == 'F')
    	{
    		a = find(a) , b = find(b);
    		if(a != b)
    		{
    			p[b] = a;
			}
		}
		else
		{
			int aa = find(a) , bb = find(b);
			int c = find(a + n) , d = find(b + n);
			if(bb != c)  p[c] = bb;
			if(d != aa)  p[d] = aa;
		}
	}
	for(int i = 1 ; i <= n ; i ++)
	{
		if(p[i] == i)  ct ++;  
	}
	cout << ct << endl;
}

字典树模板

在这里插入图片描述在这里插入图片描述

#include <iostream>

using namespace std;

const int N = 100010;

int son[N][26], cnt[N], idx;
char str[N];

void insert(char *str)
{
    int p = 0;
    for (int i = 0; str[i]; i ++ )
    {
        int u = str[i] - 'a';
        if (!son[p][u]) son[p][u] = ++ idx;
        p = son[p][u];
    }
    cnt[p] ++ ;
}

int query(char *str)
{
    int p = 0;
    for (int i = 0; str[i]; i ++ )
    {
        int u = str[i] - 'a';
        if (!son[p][u]) return 0;
        p = son[p][u];
    }
    return cnt[p];
}

int main()
{
    int n;
    scanf("%d", &n);
    while (n -- )
    {
        char op[2];
        scanf("%s%s", op, str);
        if (*op == 'I') insert(str);
        else printf("%d\n", query(str));
    }

    return 0;
}

最大异或对【字典树】

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010, M = 3100010;

int n;
int a[N], son[M][2], idx;

void insert(int x)
{
    int p = 0;
    for (int i = 30; i >= 0; i -- )
    {
        int &s = son[p][x >> i & 1];
        if (!s) s = ++ idx;
        p = s;
    }
}

int search(int x)
{
    int p = 0, res = 0;
    for (int i = 30; i >= 0; i -- )
    {
        int s = x >> i & 1;
        if (son[p][!s])
        {
            res += 1 << i;
            p = son[p][!s];
        }
        else p = son[p][s];
    }
    return res;
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i ++ )
    {
        scanf("%d", &a[i]);
        insert(a[i]);
    }

    int res = 0;
    for (int i = 0; i < n; i ++ ) res = max(res, search(a[i]));

    printf("%d\n", res);

    return 0;
}

P3375 【模板】KMP字符串匹配

#include <iostream>
#include<cstring>
using namespace std;

const int N = 100010, M = 1000010;

int n, m;
int ne[N];
char s[M], p[N];

int main()
{
	cin >> s + 1 >>  p + 1; 
    m = strlen(s + 1);
    n = strlen(p + 1);
    for (int i = 2, j = 0; i <= n; i ++ )
    {
        while (j && p[i] != p[j + 1]) j = ne[j];
        if (p[i] == p[j + 1]) j ++ ;
        ne[i] = j;
    }

    for (int i = 1, j = 0; i <= m; i ++ )
    {
        while (j && s[i] != p[j + 1]) j = ne[j];
        if (s[i] == p[j + 1]) j ++ ;
        if (j == n)
        {
            printf("%d\n", i - n + 1);
            j = ne[j];
        }
    }
    for(int i = 1 ; i <= n ; i ++)  printf("%lld " , ne[i]);
    return 0;
}





字符串哈希

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
const int P =1331;
const int P1 =1331;
const int mod1=19660813;
const int mod2=998244353;

#define endl "\n"
typedef pair<int, int> PII;
#define x first
#define y second
#define ull unsigned long long
const int inf = 1e9;
const int N = 4e5 + 10;
struct node {
    string s;
    int len;
} a[N];
int n;
bool cmp(node a, node b) { return a.len < b.len; }
PII h[N], p[N];
PII get(int l, int r) {
     int x=(h[r].x - h[l - 1].x * p[r - l + 1].x%mod1+mod1)%mod1;
     int y=(h[r].y - h[l - 1].y * p[r - l + 1].y%mod2+mod2)%mod2;
     return {x,y}; 
}

signed main() {
    map<PII, int> mp;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i].s;
        a[i].len = a[i].s.size();
    }
    sort(a+1,a+n+1,cmp);
    p[0].x = 1;
    p[0].y = 1;
    for(int i=1;i<N;i++) {
        p[i].x=p[i-1].x*P%mod1;
        p[i].y=p[i-1].y*P1%mod2;
    }
    int res = 0;
    for (int i = 1; i <= n; i++) {
        string str = a[i].s;
        str = "#" + str;
        for (int j = 1; j <= a[i].len; j++) {
            h[j].x = (h[j - 1].x * P%mod1 + str[j])%mod1;
            h[j].y = (h[j - 1].y * P1%mod2 + str[j])%mod2;
        }
        for(int j=1,k=a[i].len;j<k;j++,k--) {
            if(get(1,j)==get(k,a[i].len)) {
                res+=mp[get(j+1,k-1)];
            }
        }
        res+=mp[get(1,a[i].len)];
        mp[get(1,a[i].len)]++; 
    }
    
    cout << res << endl;

    return 0;
}

[E] Yfan_交流密文(Hard Version)

在这里插入图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值