0
点赞
收藏
分享

微信扫一扫

2021春季每日一题【week6 未完结】

云竹文斋 2022-02-26 阅读 48

目录

28. 实现 strStr()【KMP】

在这里插入图片描述

class Solution {
public:
    int strStr(string s, string p) {
        if(p.empty()) return 0;
        int n=s.size(),m=p.size();
        s=' '+s,p=' '+p;
        
        vector<int> next(m+1);
        for(int i=2,j=0;i<=m;i++)
        {
        	while(j&&p[i]!=p[j+1]) j=next[j];
        	if(p[i]==p[j+1]) j++;
        	next[i]=j;
        }
        
        for(int i=1,j=0;i<=n;i++)
        {
        	while(j&&s[i]!=p[j+1]) j=next[j];
        	if(s[i]==p[j+1]) j++;
        	if(j==m) return i-m;
        }
        return -1;
    }
};

141. 周期【KMP 未完成】

在这里插入图片描述

91. 解码方法【未完成】

在这里插入图片描述

821. 跳台阶

在这里插入图片描述

#include<cstdio>
#include<iostream>
using namespace std;
int n;
int f(int n)
{
    return (n==1||n==2)?1:f(n-1)+f(n-2);
}
int main(void)
{
    cin>>n;
    cout<<f(n+1)<<endl;
    return 0;
}

363. 矩形区域不超过 K 的最大数值和【前缀和】

在这里插入图片描述

class Solution {
public:
    int maxSumSubmatrix(vector<vector<int>>& matrix, int k)
    {
        int s[105][105]={0};
        int n=matrix.size();
        int m=matrix[0].size();
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++) s[i+1][j+1]=matrix[i][j];
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++) 
                s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
        int ans=-1e9;
        for(int i=1;i<=n;i++)
        {
            for(int j=i;j<=n;j++)
            {
                for(int l=1;l<=m;l++)
                {
                    for(int r=l;r<=m;r++)
                    {
                        int x=i,y=l;
                        int xx=j,yy=r;
                        int sum=s[xx][yy]-s[x-1][yy]-s[xx][y-1]+s[x-1][y-1];
                        if(sum<=k) ans=max(ans,sum);
                    }
                }
            }
        }
        return ans;
    }
};

3412. 邻域均值【前缀和】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int s[N][N],n,L,r,t;
int main(void)
{
    cin>>n>>L>>r>>t;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++) 
            cin>>s[i][j],s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
    int cnt=0;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            int x=max(1,i-r),y=max(1,j-r);
            int xx=min(n,i+r),yy=min(n,j+r);
            int sum=s[xx][yy]-s[x-1][yy]-s[xx][y-1]+s[x-1][y-1];
            int m=(xx-x+1)*(yy-y+1);
            if(m*t>=sum) cnt++;
        }
    cout<<cnt;
    return 0;
}

368. 最大整除子集【未完成】

在这里插入图片描述

12. 背包问题求具体方案【DP】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int f[N][N],w[N],v[N],n,m;
int path[N];
int main(void)
{
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
    for(int i=n;i>=1;i--)
    {
        for(int j=0;j<=m;j++)
        {
            f[i][j]=f[i+1][j];
            if(j>=v[i]) f[i][j]=max(f[i][j],f[i+1][j-v[i]]+w[i]);
        }
    }
    int cnt=0;
    for(int i=1,j=m;i<=n;i++)
    {
        if(j>=v[i]&&f[i][j]==f[i+1][j-v[i]]+w[i])
        {
            path[cnt++]=i;
            j-=v[i];
        }
    }
    for(int i=0;i<cnt;i++) cout<<path[i]<<" ";
    return 0;
}

377. 组合总和 Ⅳ【DP】

在这里插入图片描述

class Solution {
public:
    int combinationSum4(vector<int>& nums, int target) 
    {
        unsigned int f[100005]={0};
        f[0]=1;
        for(int i=1;i<=target;i++)
        {
            for(int j=0;j<nums.size();j++) 
                if(i>=nums[j]) f[i]+=f[i-nums[j]];
        }
        return f[target];
    }
};

3382. 整数拆分【DP】

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
const int mod=1e9;
int a[25],n;
void init()
{
    a[1]=1;
    for(int i=2;i<=21;i++) a[i]=a[i-1]*2;
}
int f[10000005];
int main(void)
{
    init();
    cin>>n;
    f[0]=1;
    for(int i=1;i<=21;i++)
    {
        for(int j=a[i];j<=n;j++)
        {
            f[j]=(f[j]+f[j-a[i]])%mod;
        }
    }
    cout<<f[n];
    return 0;
}

897. 递增顺序搜索树【模拟】

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* increasingBST(TreeNode* root) 
    {
        TreeNode* head=new TreeNode(-1);
        auto temp=head;
        dfs(root,temp);
        return head->right;
    }
    void dfs(TreeNode* root,TreeNode* &temp)
    {
        if(!root) return;
        if(root->left) dfs(root->left,temp);
        temp->right=new TreeNode(root->val);
        temp=temp->right;
        if(root->right) dfs(root->right,temp);
    }
};

47. 二叉树中和为某一值的路径【dfs】

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector< vector<int> >ans;
    vector<int>path;
    void dfs(TreeNode* root,int sum,int x)
    {
        if(!root) return;
        sum+=root->val;
        
        if(!root->left&&!root->right)
        {
            path.push_back(root->val);
            if(sum==x) ans.push_back(path);
            path.pop_back();
            return;
        }
        
        path.push_back(root->val);
        if(root->left) dfs(root->left,sum,x);
        path.pop_back();
        
        path.push_back(root->val);
        if(root->right) dfs(root->right,sum,x);
        path.pop_back();
    }
    vector<vector<int>> findPath(TreeNode* root, int x) 
    {
        dfs(root,0,x);
        return ans;
    }
};
举报

相关推荐

0 条评论