ACM日常训练日记——7.24

  • Atcoder训练
    1. Template Matching
      暴力枚举,问题给出的nxn字符串数组有没有包含下面的mxm数组
#include <bits/stdc++.h>
using namespace std;

using ll =long long;

int n,m;

int main(){
	int n,m;
	cin>>n>>m;
	vector<string>v(n+1);
	vector<string>s(n+1);
	for(int i=1;i<=n;i++){
		cin>>v[i];
	}
	for(int i=1;i<=m;i++){
		cin>>s[i];
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		if(v[i].find(s[1])!=string::npos){
			int k=i;
			int j=1;
			while(k<=m){
				if(v[k].find(s[j])!=string::npos){
					ans++;
					k++;
					j++;
				}else{
					ans=0;
					k=0;
					j=0;
					break;
				} 
			}
			if(ans==m){
			cout<<"Yes";
			return 0;
			}
		}
	}
	cout<<"No";
	return 0;
}
  1. Double Factorial
    思维,看出来结果只与乘积里面为十的倍数有关,当n为奇数,里面没有偶数,所以答案恒为零,在偶数中我们只需要找到10的因子,只有2和5
    2的因子数量肯定远远多于5的,所以我们只需要计算五的因子数有多少个即可,需要注意的是每次计算要除2,才满足是10的因子
    不断将n除以 5 并累加结果,我们可以计算出因子 5 的个数
#include <bits/stdc++.h>
using namespace std;

using ll =long long;
ll n,m;
unordered_map<ll,ll>mp;
int ans=0;
int main(){
	ll n;
	cin>>n;
	if(n%2!=0){
		cout<<0;
	}else{
		n/=2;
		ll ans=0;
		while(n>0){
			n/=5;
			ans+=n;
		}
		cout<<ans;
	}
	return 0;
}
  1. ID
    我TLE了,我用set去存放大小的时候,记录下标位置特别麻烦,只能两个for循环去找,特别慢
    优化思路想着把下标存进后面的值就成功,后面看了别人的代码又开一个map去存放下标就可以了
    我的代码
    优化
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll v[100010], d[100010];
map<pair<ll, ll>, ll> ans;

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	ll n, m;
	cin >> n >> m;
	map<ll, set<ll>> mp;
	map<ll, map<ll, ll>> position; // 存储每个元素在set中的位置
	for (int i = 1; i <= m; i++) {
		ll x, y;
		cin >> x >> y;
		v[i] = x, d[i] = y;
		mp[x].insert(y);
	}
	// 预先计算每个元素在set中的位置
	for (auto &p : mp) {
		ll x = p.first;
		int pos = 1;
		for (auto it = p.second.begin(); it != p.second.end(); ++it) {
			position[x][*it] = pos++;
		}
	}
	// 直接获取每个元素在set中的位置
	for (int i = 1; i <= m; i++) {
		d[i] = position[v[i]][d[i]];
	}
	
	for (int i = 1; i <= m; i++) {
		printf("%06lld%06lld\n", v[i], d[i]);
	}
}

  • 牛客萌新联赛

    1. 国际旅行Ⅰ
      送分我还wa了三,原因是第二次输入是m个我写成了n,死活没看到,题目已经全部联通,不需要考虑,排序就行
#include <bits/stdc++.h>
using namespace std;

using ll =long long;
ll n,m,q;
unordered_map<ll,ll>mp;
ll v[100010];
int ans=0;
ll d,e;
int main(){
	cin>>n>>m>>q;
	for(int i=0;i<n;i++)cin>>v[i];
	sort(v,v+n);
	for(int i=1;i<=m;i++)cin>>d>>e;
	while(q--){
		ll f;
		cin>>f;
		cout<<v[f-1]<<'\n';
	}
	
	return 0;
}

2.水灵灵的小学弟

名字都是一样的DHY,注意有t组输出,我wa了一次

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

using ll =long long;
ll n,m;
unordered_map<ll,ll>mp;
int ans=0;

int main(){
	
	cin>>n;
    while(n--)
	cout<<"DHY"<<'\n';
	return 0;
}

3.重生之zbk要拿回属于他的一切
之前写过差不多一模一样,函数可以留着用,就记录连续子串个数,我记得最简单可以四行,我忘了...

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

using ll =long long;
ll n,m;
unordered_map<ll,ll>mp;
int ans=0;
ll countSubstringOccurrences(const string& str, const std::string& substring) {
	ll count = 0; 
	size_t pos = str.find(substring); 
	while (pos != string::npos) {
		count++; 
		pos = str.find(substring, pos + 1); 
	}
	return count;
}

int main(){
	ll n;
	cin>>n;
	string s;
	cin>>s;
	cout<<countSubstringOccurrences(s,"chuan");
	return 0;
}

4.这是签到
计算行列式的值,线性代数,记得公式用递推就行,n,m的值不大于五,其实枚举也行,我写递推会快一点

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

using ll =long long;
ll n,m,q;
unordered_map<ll,ll>mp;
ll v[10][10];
ll d,e;

// 计算行列式的值
ll determinant(vector<vector<ll>>& matrix, ll n) {
	if (n == 1) {
		return matrix[0][0];
	} else if (n == 2) {
		return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0];
	}
	
	ll det = 0;
	for (int p = 0; p < n; ++p) {
		vector<vector<ll>> subMatrix(n - 1, vector<ll>(n - 1));
		for (int i = 1; i < n; ++i) {
			ll colIndex = 0;
			for (int j = 0; j < n; ++j) {
				if (j == p) continue;
				subMatrix[i - 1][colIndex] = matrix[i][j];
				colIndex++;
			}
		}
		det += (p % 2 == 0 ? 1 : -1) * matrix[0][p] * determinant(subMatrix, n - 1);
	}
	return det;
}

int main() {
	int n, m;
	cin >> n >> m;
	vector<vector<ll>> matrix(n, vector<ll>(m));
	for (int i = 0; i < n; ++i) {
		for (int j = 0; j < m; ++j) {
			cin >> matrix[i][j];
		}
	}
	
	ll maxSize = max(n, m);
	ll minDet = 2e18;
	
	for (int size = 1; size <= maxSize; ++size) {
		vector<vector<ll>> subMatrix(size, vector<ll>(size, 0));
		for (int i = 0; i < size; ++i) {
			for (int j = 0; j < size; ++j) {
				if (i < n && j < m) {
					subMatrix[i][j] = matrix[i][j];
				} else {
					subMatrix[i][j] = 0;
				}
			}
		}
		if (size <= n && size <= m) {
			minDet = min(minDet, determinant(subMatrix, size));
		}
	}
	
	cout << minDet << endl;
	return 0;
}

5.狼狼的备忘录
不难,只是题目给的比较坑,把第一个要求弄明白就行,然后可以用map结合string和set来保持字典序即可

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

using ll =long long;
ll n,m,q;
//unordered_map<ll,ll>mp;
ll v[10][10];
ll d,e;

//第一个条件....
bool pd(const string  a, const string  b) {
	if (a.size() > b.size()) return false;
	return b.substr(b.size() - a.size()) == a;
}

int main() {
	int n;
	cin >> n;
	map<string, set<string>> mp;
	for (int i = 0; i < n; ++i) {
		string name;
		int op;
		cin >> name >> op;
		for (int j = 0; j < op; ++j) {
			string info;
			cin >> info;
			mp[name].insert(info);
		}
	}
	
	
	for (auto& pair : mp) {
		set<string>& infos = pair.second;
		set<string> toRemove;
		for (const string& info : infos) {
			for (const string& other : infos) {
				if (info != other && pd(info, other)) {
					toRemove.insert(info);
				}
			}
		}
		for (const string& info : toRemove) {
			infos.erase(info);
		}
	}
	
	// 输出结果
	cout << mp.size() << endl;
	for (const auto& pair : mp) {
		cout << pair.first << " " << pair.second.size();
		for (const string& info : pair.second) {
			cout << " " << info;
		}
		cout << '\n';
	}
	
	return 0;
}

6.A*BBBB
这道题一般的高精度乘法超时,直接用更快的FFT优化算法
数据开大一点,就是板子了

查看代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define L(x) (1 << (x))
const double PI = acos(-1.0);
const int Maxn = 13300015;
double ax[Maxn], ay[Maxn], bx[Maxn], by[Maxn];
char sa[Maxn/2],sb[Maxn/2];
int sum[Maxn];
int x1[Maxn],x2[Maxn];
int revv(int x, int bits)
{
    int ret = 0;
    for (int i = 0; i < bits; i++)
    {
        ret <<= 1;
        ret |= x & 1;
        x >>= 1;
    }
    return ret;
}
void fft(double * a, double * b, int n, bool rev)
{
    int bits = 0;
    while (1 << bits < n) ++bits;
    for (int i = 0; i < n; i++)
    {
        int j = revv(i, bits);
        if (i < j)
            swap(a[i], a[j]), swap(b[i], b[j]);
    }
    for (int len = 2; len <= n; len <<= 1)
    {
        int half = len >> 1;
        double wmx = cos(2 * PI / len), wmy = sin(2 * PI / len);
        if (rev) wmy = -wmy;
        for (int i = 0; i < n; i += len)
        {
            double wx = 1, wy = 0;
            for (int j = 0; j < half; j++)
            {
                double cx = a[i + j], cy = b[i + j];
                double dx = a[i + j + half], dy = b[i + j + half];
                double ex = dx * wx - dy * wy, ey = dx * wy + dy * wx;
                a[i + j] = cx + ex, b[i + j] = cy + ey;
                a[i + j + half] = cx - ex, b[i + j + half] = cy - ey;
                double wnx = wx * wmx - wy * wmy, wny = wx * wmy + wy * wmx;
                wx = wnx, wy = wny;
            }
        }
    }
    if (rev)
    {
        for (int i = 0; i < n; i++)
            a[i] /= n, b[i] /= n;
    }
}
int solve(int a[],int na,int b[],int nb,int ans[])
{
    int len = max(na, nb), ln;
    for(ln=0; L(ln)<len; ++ln);
    len=L(++ln);
    for (int i = 0; i < len ; ++i)
    {
        if (i >= na) ax[i] = 0, ay[i] =0;
        else ax[i] = a[i], ay[i] = 0;
    }
    fft(ax, ay, len, 0);
    for (int i = 0; i < len; ++i)
    {
        if (i >= nb) bx[i] = 0, by[i] = 0;
        else bx[i] = b[i], by[i] = 0;
    }
    fft(bx, by, len, 0);
    for (int i = 0; i < len; ++i)
    {
        double cx = ax[i] * bx[i] - ay[i] * by[i];
        double cy = ax[i] * by[i] + ay[i] * bx[i];
        ax[i] = cx, ay[i] = cy;
    }
    fft(ax, ay, len, 1);
    for (int i = 0; i < len; ++i)
        ans[i] = (int)(ax[i] + 0.5);
    return len;
}
string mul(string sa,string sb)
{
    int l1,l2,l;
    int i;
    string ans;
    memset(sum, 0, sizeof(sum));
    l1 = sa.size();
    l2 = sb.size();
    for(i = 0; i < l1; i++)
        x1[i] = sa[l1 - i - 1]-'0';
    for(i = 0; i < l2; i++)
        x2[i] = sb[l2-i-1]-'0';
    l = solve(x1, l1, x2, l2, sum);
    for(i = 0; i<l || sum[i] >= 10; i++) // 进位
    {
        sum[i + 1] += sum[i] / 10;
        sum[i] %= 10;
    }
    l = i;
    while(sum[l] <= 0 && l>0)    l--; // 检索最高位
    for(i = l; i >= 0; i--)    ans+=sum[i] + '0'; // 倒序输出
    return ans;
}
int main()
{
    int t;
    cin>>t;
    cin.sync_with_stdio(false);
    while(t--){
    string a,b;
    cin>>a>>b;  cout<<mul(a,b)<<endl;
    }
    return 0;
}
  • 动态规划专项训练
    第二种类型没有明确的起点和终点,是连续的必须选择一个丢掉,每一次的状态要么从当前新开始a[i],要么必须选择f[i-1]+a[i].
    1.最大子串和
    这是典型的第二种类型,不知道起点终点,每一次都有可能是当前的值,否则就是前面的选上加当前,取max
#include <bits/stdc++.h>
using namespace std;

using ll =long long;
ll f[1000005];
ll F[1000005];
ll v[1000005];
map<pair<ll,ll>,ll>ans;
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	ll n,m;
	cin>>n;
    ll ma=0;
	for(int i=0;i<n;i++)cin>>v[i];
    for(int i=0;i<n;i++){
        f[i]=max(f[i-1]+v[i],v[i]);
        ma=max(f[i],ma);
    }
	cout<<ma;
}

2.最长不下降子序列
一样的解法,但是这里是长度,每个自己的长度为1,每一次更新都是跟前面比它小的判断这道题提交不了了,题没了

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

using ll =long long;
ll v[100010],d[100010];
ll f[100010];
map<pair<ll,ll>,ll>ans;
int main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int n;
	cin>>n;
	ll ans=0;
	for(int i=0;i<n;i++)cin>>v[i];
	for(int i=0;i<n;i++)f[i]=1;
	for(int i=0;i<n;i++){
		for(int j=0;j<i;j++){
			if(v[i]>v[j]){
			f[i]=max(f[j]+1,f[i]);
			ans=max(f[i],ans);
			}
		}
	}
	cout<<ans;
}
posted @ 2024-07-24 21:04  冬天的睡袋  阅读(16)  评论(0编辑  收藏  举报