其他分享
首页 > 其他分享> > DSA 2020 8.1-8.10

DSA 2020 8.1-8.10

作者:互联网

8.1

PAT dfs+dijkstra 重做

// PAT 1018
#include<stdio.h>
#include<vector> 
#include<map>
#include<algorithm>
using namespace std;
const int N = 600;
const int INF = 0x3f3f3f3f;
const int ddd = 0; 
//const int ddd = 1; 
int w[N][N];
int v[N];
int n,m,c,t;
vector<int >pre[N];
bool vis[N];
int dis[N];
vector<int > path;
vector<int > temppath;
int ans = INF; 
int minneed = INF;
int minhas = -INF;
void dij(){
	for(int i = 0;i<=n;i++){
		dis[i] = INF;
	} 
	dis[0] = 0;
	for(int i = 0;i<=n;i++){
		int k = -7;
		int _min = INF;
		for(int i = 0;i<=n;i++){
			if(!vis[i]&&dis[i]<_min){
				_min = dis[i];
				k = i;
			}
		}
		vis[k] = 1;
		for(int i = 0;i<=n;i++){
			if(dis[k]+w[k][i]==dis[i]){
				pre[i].push_back(k);
			}else if(dis[k]+w[k][i]<dis[i]){
				dis[i] = dis[k]+w[k][i];
				vector<int >().swap(pre[i]);
				pre[i].push_back(k);
			}
		}
	}
}
void dfs(int cur){
	if(ddd)printf("%d\n",cur);
	temppath.push_back(cur);
	if(cur == 0){
		int has = 0;
		int need = 0;
		for(int i = temppath.size()-1;i>=0;i--){
			// 遍历这条路径 
			int at = temppath[i];
			if(v[at]<0){
				if(has > -v[at]){
					// 注意是负数 
					has += v[at];
				}else{
					need += -v[at] - has;
					has = 0;
				} 
				if(need<minneed){
					minneed = minneed;
				}
			}else if(v[at] >=0){
				has += v[at];
			}
			if(ddd)printf("\tv[at] = %d has = %d need = %d\n",v[at],has,need);
		} 
		// 遍历路径结束
		if(need < minneed){
			path = temppath;
			minneed = need;
			minhas = has; 
		} else if(need == minneed && has < minhas){
			// 第二标尺
			path = temppath;
			minhas = has;
		}
	}
	for(int i = 0;i<pre[cur].size();i++){
		dfs(pre[cur][i]);
	}
	temppath.pop_back();
}
int main(){
	scanf("%d %d %d %d",&c,&n,&t,&m);
	v[0] = 0;
	for(int i = 1;i<=n;i++){
		scanf("%d",&v[i]);
		v[i] = v[i] - c/2;
	}
	for(int i = 0;i<N;i++){
		for(int j = 0;j<N;j++){
//			w[i][j] = -3;
			w[i][j] = INF;
		}
	}
	for(int i = 0;i<m;i++){
		int u,v,ww;
		scanf("%d %d %d",&u,&v,&ww);
		w[u][v] = w[v][u] = ww;
	} 
	dij();
	dfs(t);
	// 输出
	printf("%d ",minneed);
	for(int i = path.size()-1;i>=1;i--){
		printf("%d->",path[i]);
	} 
	printf("%d ",path[0]);
	printf("%d",minhas);
	return 0;
} 

检查出的错误

  1. need += -v[at] - has; 之前写成了 need = -v[at] - has; 忘记累加了
  2. for(int i = path.size()-1;i>=1;i++)
  3. 命名不规范,身体的时候就要看好是max 还是 min

8.2

正则表达式匹配

class Solution {
public:
    string _s;
    string _p;
    bool match(int ii ,int jj){
        // printf("%s\n%s\n",_s.c_str(),_p.c_str());
        return (_p[jj] == '.' || _s[ii] == _p[jj]) && ii!=0;
    }
    bool isMatch(string s, string p) {
        int lens = s.size();
        int lenp = p.size();
        s.insert(0,"#");
        p.insert(0,"#");
        _s = s;
        _p = p;
        vector<vector<int > > dp(lens+10,vector<int >(lenp+10,false));
        dp[0][0] = true;
        // 注意这里的循环
        for(int i = 0;i<=lens;i++){
            for(int j = 0;j<=lenp;j++){
                // 不要害怕越界
                if(i == 0&& j == 0)continue;
                // 应该把匹配写成一个函数,避免越界问题
                // if(s[i] == p[j] || p[j] == '.'){
                if(match(i,j)){
                    if(i>=1 && j>=1)dp[i][j] = dp[i-1][j-1];
                }
                else{
                    // 特判
                    if(p[j] == '*'){
                        // if(j>0 && (s[i] == p[j-1] || p[j-1] == '.')){
                        if(j>0 && match(i,j-1)){
                            // 匹配或者不匹配都行
                            // 重点理解
                            if(i>=1)dp[i][j] = dp[i-1][j];
                            if(j>=2)dp[i][j] |= dp[i][j-2];
                        }else{
                            if(j>=2)dp[i][j] = dp[i][j-2];
                        }
                    }else{
                        dp[i][j] = false;
                    }
                }
            }
        }
        return dp[lens][lenp];
    }
};

8.3

编辑距离

一遍过

标签:8.1,right,TreeNode,cur,int,2020,return,8.10,dp
来源: https://www.cnblogs.com/zxyfrank/p/13938899.html