其他分享
首页 > 其他分享> > PAT甲级2020冬季

PAT甲级2020冬季

作者:互联网

PAT甲级2019春季

// 02
#include<iostream>
#include<algorithm>
#include<set>
#include<unordered_map>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;

int n, m, k, cnt = 0;
string str, tar;

int main(){
	cin >> str;
	cin >> tar;
//	cout << str.length();
	string minn;
	for(int i = 0 ; i < str.length(); i++){
//		cout << str[i] << endl;
		int pos = 0; string temp;
		if(str[i] == tar[pos]) pos++;
		for(int j = i+1; j < str.length(); j++){
			if(str[j] == tar[pos]) pos++;
			if(pos == tar.length()){
				cnt++;
				temp = str.substr(i, j-i+1);
				if(cnt == 1) minn = temp;
				if(temp.length() < minn.length()) minn = temp;
			}
		}
	}
	cout << minn;
    return 0;
}

//03
#include<iostream>
#include<algorithm>
#include<set>
#include<unordered_map>
#include<queue>
#include<cstring>
#include<vector>
using namespace std;

int n, m, k, cnt = 0;
string str, tar;

int root;
int parent[10009];

unordered_map<int, int> visit;
vector<int> levelnode[1009];

void DFS(int a){
	if(a == root){
		printf("%04d", root);
		return;
	}
	DFS(parent[a]);
	printf("->%04d", a);
} 

int main(){
	scanf("%d\n", &n);
	for(int i = 0 ; i < n ; i++){
		string temp;
		getline(cin, temp);
		
		int lev = temp.length();
		int t = stoi(temp);
		visit[t] = 1;
		levelnode[lev].emplace_back(t);
		
		if(i == 0){
			root = t;
			parent[root] = -1;
		}else
			parent[t] = levelnode[lev-1][levelnode[lev-1].size()-1];
	}
	scanf("%d", &m);
	vector<int> ans;
	for(int i = 0 ; i < m ; i++){
		int a;
		cin >> a;
		if(visit[a] == 0){
			printf("Error: %04d is not found.\n", a);
		}else{
			DFS(a);
			printf("\n");
		}
		ans.clear();
	}
    return 0;
}

标签:PAT,tar,temp,int,甲级,2020,str,include,root
来源: https://blog.csdn.net/qq_42871911/article/details/121609064