文章目录
- 1.A. Common Prefixes-字符串
- 2.B1. Koa and the Beach (Easy Version)-贪心/dfs
- 3.B2. Koa and the Beach (Hard Version)-贪心
- 4.C. String Transformation 1-字符串
A. Common Prefixes
每次输出和上一个字符串有相同p长度前缀的字符串;
每次输入前缀字符个数p,让共同前缀下一位变更即可;
#include
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;
}
B1. Koa and the Beach (Easy Version)
①贪心
#include
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
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;
}
B2. Koa and the Beach (Hard Version)
贪心(和B1一样)
#include
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;
}
C. String Transformation 1
描述:将字符串A中相同字母(大于等于1个)换成ASCⅡ码序大的字母;最终要求变成字符串B,不可以输出“-1”,可以输出最小改的次数。(字母由a到t)
思路:
字母从a开始遍历到t;
每一步将A中相同字母变为–>这些相同字母对应B中字母的最小字母;
如果A和B对应字母相同不变;
#include
#include
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;
}