河南萌新联赛2024第(二)场 AEFHIJ题解

 A   国际旅行Ⅰ

思路:据题可知,所有国家相通,所以排序后输出即可

AC代码:

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

int n,m,q,k;
int arr[1003];

int main(){
    cin>>n>>m>>q;
    for(int i=1;i<=n;i++) cin>>arr[i];
    sort(arr+1,arr+1+n);
    for(int i=1;i<=m;i++){
        int aa,bb;
        cin>>aa>>bb;
    }
    while(q--){
        cin>>k;
        cout<<arr[k]<<endl;
    }
    return 0;
}

E   “好”字符

思路:将第二个字符串后再加一个第二字符串,方便 循环同构的操作,枚举出现过的字母,每次枚举中将两个字符串中非当前枚举字符的字符变为 ‘*’ ,然后判断第一字符串是否为第二字符串的子串即可    注意:这里用find()会TLE,所以要使用前缀函数+kmp算法来解决

AC代码:

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

int n;
string s1,s2;

vector<int> prefix_function(const string &s){
    int n = s.size();
    vector<int> pi(n);
    for(int i = 1, j; i < n; i ++) {
        j = pi[i - 1];
        while(j > 0 and s[i] != s[j]) j = pi[j - 1];
        if(s[i] == s[j]) j ++;
        pi[i] = j;
    }
    return pi;
}
 
 
bool kmp(const string &text, const string &pattern) {
    string cur = pattern + "#" + text;
    int n = text.size(), m = pattern.size();
    vector<int> lps = prefix_function(cur);
    for(int i = m + 1; i <= n + m; i ++) {
        if(lps[i] == m) return true;
    }
    return false;
}

int main(){
    cin>>n;
    cin>>s1;
    cin>>s2;
    s2 += s2;
    vector<int> cnt1('z'+1),cnt2('z'+1);
    for(auto i:s1) cnt1[i]=1;
    for(auto i:s2) cnt2[i]=1;

    int ans = 0;
    for(char c='a';c<='z';c++){
        if(cnt1[c]==0 || cnt2[c]==0) continue;
        string aa = s1 , bb = s2;
        for(auto& i:aa) if(i!=c) i='*';
        for(auto& i:bb) if(i!=c) i='*';
        if(kmp(bb,aa)) ans++;
    }
    cout<<ans<<endl;
    return 0;
}

F   水灵灵的小学弟

究极整活题.........

思路:伪装成了博弈论,但双方名字缩写都是DHY,所以无论中间发生了什么 不 可 描 述 的事情,输出DHY即可

AC代码:

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

int main(){
    int n;
    cin>>n;
    while(n--){
        int a,b;
        cin>>a>>b;
        cout<<"DHY"<<endl;
    }
    return 0;
}

H   狼狼的备忘录

思路: 用map<string,vector<string> >来维护,map自带排序,vector可以方便的排序与删改

AC代码:

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

bool endsWith(string str,string suffix) {
    if (suffix.length() > str.length()) { return false; }
 
    return (str.rfind(suffix) == (str.length() - suffix.length()));
}

map<string, vector<string> >peo;

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        string namm;
        int x;
        string aa;
        cin>>namm>>x;
        for(int j=0;j<x;j++){
            cin>>aa;
            peo[namm].push_back(aa);
        }
    }

    for(auto& xx:peo){
        sort(xx.second.begin(),xx.second.end());

        // for(int j=0;j<xx.second.size();j++) cout<<xx.second[j]<<" ";
        // cout<<endl;

        for(int j=0;j<xx.second.size();j++){
            for(int k=0;k<xx.second.size();k++){
                if(k!=j && endsWith(xx.second[k],xx.second[j]) ){
                    xx.second.erase(xx.second.begin()+j); j--;//注意这里删了一个,j要退一格
                    break;
                }
            }
        }
    }
    cout<<peo.size()<<endl;
    for(auto& xx:peo){
        cout<<xx.first<<" "<<xx.second.size()<<" ";
        for(int j=0;j<xx.second.size();j++) cout<<xx.second[j]<<" ";
        cout<<endl;
    }
    return 0;
}

 I   重生之zbk要拿回属于他的一切

按题意找就行......

AC代码:

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

int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    int cnt = 0;
    for(int i=0;i<=n-5;i++){
        if(s[i]=='c' && s[i+1]=='h' && s[i+2]=='u' && s[i+3]=='a' && s[i+4]=='n') cnt++;
    }
    cout<<cnt;
    return 0;
}

J   这是签到

这也不是签到...

思路:模拟行列式的运算即可,注意n==m的情况,ans的初始值不能设成0,应设成一大值

AC代码:

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

int det(int n, int** mat)
{
	int mov = 0;
	int flag;
	int sum = 0;
	if (n == 1) return mat[0][0];
	int** b = new int* [n - 1];
	for (int z = 0; z < n-1; ++z)
		b[z] = new int[n-1];
	for (int i = 0; i < n; ++i)
	{
		for (int j = 0; j < n - 1; ++j) {
			mov = i > j ? 0 : 1;
			for (int k = 0; k < n - 1; ++k)
				b[j][k] = mat[j + mov][k + 1];
		}
		if (i % 2 == 0) flag = 1;
		else flag = -1;
		sum += flag * mat[i][0] * det(n - 1, b);
	}
	delete[] b;
	return sum;
}
int main()
{
	int n,m;

	cin >> n>>m;
	int** a = new int* [5];
	for (int z = 0; z < n; ++z)
		a[z] = new int[5];

	for (int x = 0; x < n; ++x)
		for (int y = 0; y < m; ++y)
			cin >> a[x][y];
    if(n!=m){
        int ans = 0;
	    for(int i=1;i<=min(n,m);i++){
            ans = min(ans,det(i,a));
        }
        cout<<ans;
    }
    else{
        int ans = 99999999;
        for(int i=1;i<=min(n,m);i++){
            ans = min(ans,det(i,a));
        }
        cout<<ans;
    }
	for (int p = 0; p < n; ++p)
		delete[] a[p];
	delete[]a;
	return 0;
}

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值