Codeforces Round #659(Div.2)

1.A. Common Prefixes-字符串

A. Common Prefixes
每次输出和上一个字符串有相同p长度前缀的字符串;
每次输入前缀字符个数p,让共同前缀下一位变更即可;

#include<iostream>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while(t--)
    {
		int n,p;
		cin >> n;
		string m(200,97);//全赋值为'a'
		cout << m << endl;
		for(int i=0;i<n;i++)
		{
		    cin>>p;
			if(m[p]=='a')//让共同前缀下一位变更
			m[p]='b';
			else
			m[p]='a';
			cout << m <<endl;
		}
	}
	return 0;
}

2.B1. Koa and the Beach (Easy Version)-贪心/dfs

B1. Koa and the Beach (Easy Version)
①贪心

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int a[N];
void solve(){
	int n,k,l,i;
	scanf("%d%d%d",&n,&k,&l);
	a[0]=a[n+1]=k+1;
	for(i=1;i<=n;i++)
    {
		cin>>a[i];
		a[i]=l-a[i];//a数组存放剩余上涨高度
    }
	int w=k+1,f=-1;//w表示当前潮汐高度f表示下一秒潮汐变化的数字
	for(i=1;i<=n;i++)
    {
		if(a[i]<0){cout<<"No"<<endl;return;}//无法通过
		else if(a[i]>=k){w=k+1;f=-1;}//潮汐在最高处时这个点还是安全的,可以等待到最高点再走
		else if(f==-1)w=min(w+f,a[i]);//只要当前是潮落的状态就可以等待
		else{w+=f;if(w>a[i]){cout<<"No"<<endl;return;}}//若是涨潮状态这个点已经不安全了则说明这个点无法通过
		if(w==0)f=1;
	}
	cout<<"Yes"<<endl;
}

int main()
{
	int T;
	cin>>T;
	while(T--)solve();
	return 0;
}

②dfs

#include <bits/stdc++.h>

using namespace std;

int main()
{
	ios_base::sync_with_stdio(0), cin.tie(0);

	int test;
	cin >> test;
	while (test--)
	{
		int n, k, l;
		cin >> n >> k >> l;
		vector<int> d(n+2, -k);
		for (int i = 1; i <= n; ++i)
			cin >> d[i];

		set<tuple<int, int, bool>> mark;
		function<bool(int, int, bool)> go = [&](int pos, int tide, bool down)
		{
			if (pos > n) return true;
			
			if (mark.find({ pos, tide, down }) != mark.end())
				return false;

			mark.insert({ pos, tide, down });

			tide += down ? -1 : +1;
			if (tide == 0) down = false;
			if (tide == k) down = true;

			if (d[pos] + tide <= l && go(pos, tide, down))
				return true;
			if (d[pos + 1] + tide <= l && go(pos + 1, tide, down))
				return true;
			return false;
		};

		if (go(0, 0, false)) cout << "Yes\n";
		else cout << "No\n";
	}

	return 0;
}

3.B2. Koa and the Beach (Hard Version)-贪心

B2. Koa and the Beach (Hard Version)
贪心(和B1一样)

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+5;
int a[N];
void solve(){
	int n,k,l,i;
	scanf("%d%d%d",&n,&k,&l);
	a[0]=a[n+1]=k+1;
	for(i=1;i<=n;i++)
    {
		cin>>a[i];
		a[i]=l-a[i];//a数组存放剩余上涨高度
    }
	int w=k+1,f=-1;//w表示当前潮汐高度f表示下一秒潮汐变化的数字
	for(i=1;i<=n;i++)
    {
		if(a[i]<0){cout<<"No"<<endl;return;}//无法通过
		else if(a[i]>=k){w=k+1;f=-1;}//潮汐在最高处时这个点还是安全的,可以等待到最高点再走
		else if(f==-1)w=min(w+f,a[i]);//只要当前是潮落的状态就可以等待
		else{w+=f;if(w>a[i]){cout<<"No"<<endl;return;}}//若是涨潮状态这个点已经不安全了则说明这个点无法通过
		if(w==0)f=1;
	}
	cout<<"Yes"<<endl;
}

int main()
{
	int T;
	cin>>T;
	while(T--)solve();
	return 0;
}

4.C. String Transformation 1-字符串

C. String Transformation 1
描述:将字符串A中相同字母(大于等于1个)换成ASCⅡ码序大的字母;最终要求变成字符串B,不可以输出“-1”,可以输出最小改的次数。(字母由a到t)

思路:
字母从a开始遍历到t;
每一步将A中相同字母变为–>这些相同字母对应B中字母的最小字母;
如果A和B对应字母相同不变;

#include<bits/stdc++.h>
#include<iostream>
using namespace std;
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
    {
        int n,ans=0;
        cin>>n;
        string a,b;
        cin>>a>>b;
        for(int i=0;i<20;i++)//遍历20个字母
        {
            int minn=30;
            for(int j=0;j<n;j++)//找出a中当前字母对应b字母中的最小字母
                if(a[j]-'a'==i&&a[j]!=b[j]&&b[j]>a[j])minn=min(minn,b[j]-'a');
            for(int j=0;j<n;j++)//让a中当前字母变为该最小字母
                if(a[j]-'a'==i&&a[j]!=b[j]&&b[j]>a[j])a[j]=minn+'a';
            if(minn!=30)ans++;
        }
        if(a==b)cout<<ans;
        else cout<<"-1";
        cout<<endl;
    }
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值