2019 Henan ICPC Summary

Question A

analysis:

Just go through STl violence (the original purpose of the question is to investigate KMP, but the data is too watery)

Code:

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

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int ans = 0;
		string m, n, s;
		cin >> m >> s;
		n = m;
		reverse(n.begin(), n.end());
		int mlen = m.length(), slen = s.length();
		for (int i = 0; i <= slen - mlen; i++)
		{
			if (s.substr(i, mlen) == m)ans++;
			if (s.substr(i, mlen) == n)ans++;
		}
		if (n == m)ans /= 2;
		cout << ans << endl;
	}
	return 0;
}

 

Question C

analysis:

This question is not difficult, but there are many pitfalls: multiple sets of data, a sequence can only contain one codon. There is an unexpected place where you have to send wa.

Code:

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

struct In
{
	int st;
	int len;
}in[510];

int main()
{
	int n, k;
	while (cin >> n >> k)
	{
		int alen[110], tlen, cnt = 0;
		string a[110], t, s;
		for (int i = 0; i<n; i++){
			cin >> a[i];
			alen[i] = a[i].length();
		}
		cin >> t;
		tlen = t.length();
		for (int i = 0; i<tlen; i++){
			for (int j = 0; j<n; j++){
				if (i + alen[j] <= tlen&&t.substr(i, alen[j]) == a[j]){
					in[cnt++] = { i, alen[j] };
					i += alen[j] - 1;
					break;
				}
			}
		}

		for (int i = 0; i<k; i++){
			int ans = 0;
			cin >> s;
			for (int j = 0; j<cnt; j++)
				if (t.substr(in[j].st, in[j].len) != s.substr(in[j].st, in[j].len))
					ans++;
			cout << ans << endl;
		}
	}
	return 0;
}

Question E

analysis:

First build a tree to find the total number of child nodes of each node, and then traverse all nodes to sum the path. Used for child nodes are all smaller than the node number, so it will be more convenient to build a tree, just a loop to get it. The pit point is that the number of child nodes is a number in the range of int, but the data may satisfy the multiplication of two ints to cause explosion int, so use ll to store.

Code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;

struct City
{
	vector<int>v;
	ll sum;
}city[maxn];

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		ll ans = 0;
		int n;
		cin >> n;
		for (int i = 1; i <= n; i++){
			city[i].sum = 1;
			city[i].v.clear();
		}//init

		for (int i = 0; i<n - 1; i++)
		{
			int a, b;
			scanf("%d%d", &a, &b);
			if (a<b)swap(a, b);
			city[a].v.push_back(b);
		}

		for (int i = 1; i <= n; i++)
		{
			int len = city[i].v.size();
			for (int j = 0; j<len; j++)
			{
				city[i].sum += city[city[i].v[j]].sum;
			}
			for (int j = 0; j<len; j++)
			{
				ans += city[city[i].v[j]].sum*(city[i].sum - city[city[i].v[j]].sum - 1);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

Question J

analysis:

Simply find regular questions.

Code:

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

int lowbit(int a)
{
	return a&(-a);
}

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n, a, mi, ma;
		cin >> n;
		mi = n;
		while ((a = lowbit(mi)) != 1)mi -= a / 2;
		ma = n * 2 - mi;
		cout << mi << ' ' << ma << endl;
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_43700916/article/details/90448231