Hangzhou Daxi Information Technology Co., Ltd. Written Examination

Idea: The intervals of this question have been sorted according to the start time, so we only need to scan O(n) once to merge the intervals.

The two intervals in the above figure represent the meaning that the right end point of the left interval is smaller than the left end point of the right interval, and there is no overlap.

The two intervals in the above figure represent the meaning that the right end point of the left interval is greater than or equal to the left end point of the right interval, and there is overlap and can be merged.

Discuss the right end of the interval after the merged interval:

After the intervals in the above figure are merged, the right endpoint is the right endpoint of the right interval.

After the intervals in the above figure are merged, the right endpoint is the right endpoint of the left interval.

Therefore, we should take the maximum of the two right end points of the two overlapping intervals.

#include<bits/stdc++.h> 
using namespace std;

vector<vector<int>> merge(vector<vector<int>> intervals){
	int n = intervals.size(), i, IntervalL, IntervalR;
	vector<vector<int>> ans;
	IntervalL = intervals[0][0];
	IntervalR = intervals[0][1];
	for(i = 1; i < n; ++ i){
		if(intervals[i][0] <= IntervalR){
			IntervalR = max(IntervalR, intervals[i][1]);
		}else{
			vector<int> interval;
			interval.push_back(IntervalL);
			interval.push_back(IntervalR);
			ans.push_back(interval);
			IntervalL = intervals[i][0];
			IntervalR = intervals[i][1];
		}
	}
	vector<int> interval;
	interval.push_back(IntervalL);
	interval.push_back(IntervalR);
	ans.push_back(interval);
	return ans;
}

int main(){
	vector<vector<int>> Intervals;
	int n, L, R, i;
	cin >> n;
	for(i = 0; i < n; ++ i){
		cin >> L >> R;
		vector<int> interval;
		interval.push_back(L);
		interval.push_back(R);
		Intervals.push_back(interval);
	}
	Intervals = merge(Intervals);
	for(i = 0; i < Intervals.size(); ++ i){
		cout << Intervals[i][0] << " " << Intervals[i][1] << endl;
	}
	return 0;	
}

Idea: We only need to set up two separate index pointers to traverse the pattern string abbr. If it points to a letter, it directly judges whether it matches; if it points to a number, it needs to parse out the number first, and then the index of the string s The pointer moves directly back. If they match eventually, both pointers must stay at the end of the string.

#include<bits/stdc++.h>
using namespace std;

bool valid(string word, string abbr){
	int index1 = 0, index2 = 0, n1 = word.size(), n2 = abbr.size(), num;
	while( index1 < n1 && index2 < n2 ){
		if( abbr[index2] >= 'a' && abbr[index2] <= 'z' ){ // 如果是字母 
			if( word[index1] != abbr[index2] ) return false;
			++ index1;
			++ index2;
		}else{ // 如果是数字 
			num = 0;
			while( abbr[index2] >= '0' && abbr[index2] <= '9' && index2 < n2 ){
				num = num * 10 + abbr[index2] - '0';
				++ index2;
			}
			index1 += num;
		}
	}
	return index1 == n1 && index2 == n2;
}

int main(){
	string s, abbr;
	cin >> s >> abbr;
	cout << ( valid(s,abbr) ? "true" : "false" );
	return 0;
}

Idea: Directly search all paths of start-end to get results. The answer path must not go through an edge multiple times, nor will it run on the loop, because the bitwise OR operation has two values: 1. In the case of all positive integers, the result of the bitwise OR cannot become smaller. 2 . The result of bitwise OR with the same value multiple times is the same as the result of one time.

#include<bits/stdc++.h>
using namespace std;

const int MaxN = 10010;
int cnt, Head[MaxN], To[MaxN], Weight[MaxN], Next[MaxN], Ans; 
bool visited[MaxN];

void add(int from, int to, int value){
	Next[++ cnt] = Head[from];
	Head[from] = cnt;
	To[cnt] = to;
	Weight[cnt] = value;
}

void DFS(int S, int O, int OrResult){
	if(S == O){
		Ans = min(Ans, OrResult);
		return;
	}
	int to;
	for(int E = Head[S]; E != 0; E = Next[E]){
		to = To[E];
		if(!visited[to]){
			visited[to] = true;
			DFS(to, O, OrResult | Weight[E]);
			visited[to] = false;
		}
	}
}

int minPath(int n, vector<vector<int>> edges, int start, int end){
	int m = edges.size(), i, j;
	for(i = 0; i < m; ++ i){
		add(edges[i][0], edges[i][1], edges[i][2]);
	}
	Ans = 0x3f3f3f3f;
	visited[start] = true;
	DFS(start, end, 0);
	return Ans;
}

int main(){
	int n, i, u, v, w, start, end;
	cin >> n;
	vector<vector<int>> edges;
	for(i = 0; i < n; ++ i){
		vector<int> edge;
		cin >> u >> v >> w;
		edge.push_back(u);
		edge.push_back(v);
		edge.push_back(w);
		edges.push_back(edge);
	}
	cin >> n >> start >> end;
	cout << minPath(n, edges, start, end);
	return 0;
} 
//测试数据
/*
3
1 2 1
2 3 3
1 3 100
3 1 3
*/

 

Guess you like

Origin blog.csdn.net/qq_39304630/article/details/112723839