头像

Underthemoon




离线:10小时前


最近来访(5)
用户头像
Tiaer
用户头像
半夏听雨
用户头像
幽梦影情结
用户头像
Aurora-
用户头像
Caiers

活动打卡代码 AcWing 841. 字符串哈希

Underthemoon
10小时前
#include<iostream>
using namespace std;

const int N = 100010, P = 131;

typedef long long ULL;
ULL p[N],h[N];
char str[N];
int n,m;
ULL get(int l, int r)
{
    return h[r] - h[l - 1] * p[r - l + 1];
}
int main()
{
    scanf("%d%d%s",&n,&m,str + 1);
    p[0] = 1;
    for(int i = 1; i <= n ; i++) 
    {
        p[i] = p[i - 1] * P;
        h[i] = h[i - 1] * P + str[i];
    }
    while(m --)
    {
        int a,b,c,d;
        cin >> a >> b >>c >>d;
        if(get(a,b) == get(c,d)) puts("Yes");
        else puts("No");
    }
    return 0;
}


活动打卡代码 AcWing 838. 堆排序

Underthemoon
12小时前
#include<iostream>
using namespace std;
const int N = 100010;
int heap[N];
int n,m;
void push_up(int u)
{
    while(u / 2 &&heap[u] < heap[u / 2])
    {
        swap(heap[u],heap[u / 2]);
        u /= 2;
    }
}
void push_down(int u , int size)
{
    int t = u, l = u * 2, r = u * 2 + 1;
    if(l <= size && heap[t] > heap[l]) t = l;
    if(r <= size && heap[t] > heap[r]) t = r;
    if(t != u)
    {
        swap(heap[u],heap[t]);
        push_down(t,size);
    }
}
void heap_sort()
{
    int size = n;
    for(int i = 1; i <= n; i++) push_up(i);

    for(int i = 1; i <= m ; i++) 
    {
        cout << heap[1] << " ";
        heap[1] = heap[size--];
        push_down(1,size);
    }
}
int main()
{
    cin >> n >> m;
    for(int i = 1; i <= n; i++) cin >> heap[i];
    heap_sort();
    return 0;
}



活动打卡代码 AcWing 826. 单链表

#include<iostream>
using namespace std;
const int N  = 100010;
int idx,e[N],ne[N],head;
void init()
{
    head = -1;
    idx = 0;
}
void add_to_head(int x)
{
    e[idx] = x;
    ne[idx] = head;
    head = idx++;
}
void add(int k ,int x)
{
    e[idx] = x;
    ne[idx] = ne[k];
    ne[k] = idx;
    idx++;
}
void remove(int k)
{

    ne[k] = ne[ne[k]];
}
int main()
{
    init();
    int m;cin >> m;
    while(m --)
    {
        char op;cin >>op;
        int k, x;

        if(op == 'H')
        {
            cin >>x;
            add_to_head(x);
        }else if(op == 'D'){
            cin >> k;
            if(!k) head = ne[head];
            remove(k -1);

        }else{
            cin >> k >> x;
            add( k -1 ,x);
        }
    }
    for(int i = head;i!= -1;i = ne[i]) cout << e[i] << " ";
}



蓝桥杯用的是dev 5.4吗,我下载了一个5.4为什么没有代码补全,这个问题可以解决吗



活动打卡代码 AcWing 840. 模拟散列表

Underthemoon
2个月前
#include<iostream>
#include<cstring>
using namespace std;
const int N = 200010;
int h[N],null = 0x3f3f3f3f;
int find(int x)
{
    int k = ( x % N + N) % N;
    while(h[k] != null && h[k] != x)
    {
        k ++;
        if(k == N) k = 0;
    }
    return k;
}
int main()
{
    int n ; cin >> n;
    char op[2];int x;
    memset(h,null,sizeof h);
    while(n --)
    {
        scanf("%s%d",&op,&x);
        if(op[0] == 'I') h[find(x)] = x;
        else
        {
            if(h[find(x)] != null) puts("Yes");
            else puts("No");
        }
    }
}

#include<iostream>
#include<cstring>
using namespace std;
const int N = 100003;

int h[N],e[N],ne[N],idx;
int n;
void insert(int x)
{
    int k = (x % N + N) % N;
    e[idx] = x,ne[idx] = h[k], h[k] = idx++;
}
bool query(int x)
{
    int k = (x % N + N) % N;
    for(int i = h[k]; i != -1; i= ne[i])
    {
        int j = e[i];
        if(x == j) return true;
    }
    return false;
}
int main()
{
    cin >> n;
    memset(h,-1,sizeof h);
    while(n --)
    {
        char c;
        int x;
        cin >> c >> x;
        if(c == 'I') insert(x);
        else
        {
            if(query(x)) puts("Yes");
            else puts("No");
        }
    }
    return 0;
}


活动打卡代码 AcWing 830. 单调栈

Underthemoon
2个月前
#include<iostream>
using namespace std;
const int N = 100010;
int stk[N],tt,n;

int main()
{
    cin >> n;
    while(n--)
    {
        int x;cin >> x;
        while(tt && stk[tt] >= x) tt--;
        if(tt) cout << stk[tt] << " ";
        else cout << "-1 ";
        stk[++tt] = x;
    }
}


活动打卡代码 AcWing 2816. 判断子序列

Underthemoon
2个月前
#include<iostream>
using namespace std;
const int N = 100010;
int a[N],b[N],n,m;

int main()
{
    cin >> n >> m;
    for(int i = 0;i < n; i++) cin >> a[i];
    for(int i = 0; i < m; i ++) cin >> b[i];
    int i= 0,j=0;
    while(i < n && j < m)
    {
        if(a[i] == b[j]) i++;
        j++;
    }
    if(i == n) puts("Yes");
    else puts("No");
}




Underthemoon
2个月前
#include<iostream>
using namespace std;
const int N = 100010;
int a[N],b[N];
int n , m , x;

int main()
{
    cin >> n >> m >> x;
    for(int i = 0;i < n; i++) scanf("%d",&a[i]);
    for(int i = 0;i < m; i++) scanf("%d",&b[i]);

    for(int i = 0,j = m -1;i < n; i++)
    {
        while(j && a[i] + b[j] > x) j--;
        if(a[i] + b[j] == x) cout << i << " " << j;
    }
    return 0;

}



Underthemoon
2个月前
#include<iostream>
using namespace std;
int n;
int main()
{
    cin >> n;
    while(n--)
    {
        int res = 0,x;cin >> x;
        while(x)
        {
            x -= (x & -x);
            res ++ ;
        }
        cout << res << " ";
    }
    return 0;
}


活动打卡代码 AcWing 803. 区间合并

Underthemoon
2个月前
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int n;
const int N = 100010;
vector<pair<int,int>> segs,res;
void merge()
{

    sort(segs.begin(),segs.end());
    int st = -2e9,ed = -2e9;

    for(auto seg : segs)
    {
        if(ed < seg.first)
        {
            if(ed != -2e9) res.push_back({st,ed});
            st = seg.first;ed = seg.second;
        }else ed = max(ed,seg.second);
    }
    if(st != -2e9) res.push_back({st,ed});

}
int main()
{
    cin >> n;
    for(int i =0;i<n;i++)
    {
        int l,r;cin >> l >> r;
        segs.push_back({l,r});
    }

    merge();
    cout << res.size();
}