A
相同字母尽可能相邻
#include <bits/stdc++.h>
using namespace std;
char a[5] = { 'a', 'e', 'i', 'o', 'u' };
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin >> n;
string s="";
for(int i=0;i<=n-1;i++)
s+=a[i%5];
sort(s.begin(),s.end());
cout<<s<<endl;
}
return 0;
}
B1 B2
处理思路类似 模拟即可
复杂后用二分寻找边界
# include <bits/stdc++.h>
using namespace std;
#define int long long
int n,a,b,id,m,q;
signed main()
{
int T;
cin>>T;
while(T--)
{
cin>>n>>m>>q;
cin>>a>>b>>id;
if(a==id||b==id)
{
cout<<0<<endl;
continue;
}
if(a<b)swap(a,b);
if(id>=a||id<=b)
{
if(id>=a)cout<<n-a;
else if(id<=b)
cout<<b-1;
}
else if(id>b&&id<a)
{
int k=a-b>>1;
cout<<k;
}
cout<<endl;
}
return 0;
}
# include <bits/stdc++.h>
using namespace std;
#define int long long
int n,a,b,id,m,q;
vector<int>v;
signed main()
{
int T;
cin>>T;
while(T--)
{
v.clear();
cin>>n>>m>>q;
a=n,b=1;
for(int i=1;i<=m;i++)
{
int x;
cin>>x;
v.push_back(x);
}
sort(v.begin(),v.end());
while(q--)
{
cin>>id;
if(v[m-1]==id||v[0]==id)
{
cout<<0<<endl;
continue;
}
if(id>v[m-1])cout<<n-v[m-1];
else if(id<v[0])cout<<v[0]-1;
else
{
vector<int>::iterator iter1;
vector<int>::iterator iter2;
iter1=lower_bound(v.begin(), v.end(), id);
a=(*iter1);
iter2=lower_bound(v.begin(), v.end(), id)-1;
//cout<<a<<" "<<b<<endl;
b=(*iter2);
if(id>b&&id<a)
{
int k=a-b>>1;
cout<<k;
}
}
cout<<endl;
}
}
return 0;
}
C
dp[i][j]含义代表最大的分 i表示当前第i个字符串,j表示该匹配第j个字符,当满足匹配重置为零 有两种状态继承 一种不选取字符串 一种选取一个字符串并更改新的匹配字符
#include <bits/stdc++.h>
using namespace std;
int dp[1005][5];
string t = "narek";
int n, m;
pair<int,int> get_score(string s, int now)
{
int ans = 0;
for (int i = 0; i < m; i++)
{
if (s[i] == t[now])
{
now++;
if (now == 5) ans += 10, now = 0;
}
if (t.find(s[i]) != string::npos) ans--;
}
return {ans, now};
}
int main()
{
ios::sync_with_stdio(0);
int T = 1;
cin >> T;
while (T--)
{
cin >> n >> m;
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= 4; j++)
dp[i][j] = -2e9;
}
dp[0][0] = 0;
for (int i = 1; i <= n; i++)
{
string s; cin >> s;
for (int j = 0; j <= 4; j++)
{
dp[i][j] = max(dp[i][j], dp[i - 1][j]);
pair<int,int> temp = get_score(s, j);
int score = temp.first, nxt = temp.second;
dp[i][nxt] = max(dp[i][nxt], dp[i - 1][j] + score);
}
}
int mx = 0;
for (int j = 0; j < 5; j++) mx = max(mx, dp[n][j]);
cout << mx << endl;
}
}