2.3
A. Mahmoud and Longest Uncommon Subsequence
https://codeforces.com/contest/766/problem/A
题解
水题,直接判断两个字符串是否相同即可。
Code
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
const int N= 1e5+7;
const ll mod=998244353;
int main(){
IO;
int t=1;
//cin>>t;
while(t--){
string x,y;
cin>>x>>y;
if(x==y)cout<<-1<<endl;
else{
cout<<max(x.size(),y.size())<<endl;
}
}
return 0;
}
B. Mahmoud and a Triangle
https://codeforces.com/contest/766/problem/B
题解
题意为给出一个序列,求其中是否存在三个可以组成三角形边长的组合。
首先排序a,因为任意一个值都有可能作为三角形的最长边,所以遍历(从第3个值开始)序列,当a[i]作为三角形的最长边时,如果满足a[i-2]+a[i-1]>a[i],则可以组成三角形,否则一定不可能构成三角形。
Code
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
const int N= 1e5+7;
const ll mod=998244353;
int a[N];
int main(){
IO;
int t=1;
//cin>>t;
while(t--){
int n;
cin>>n;
for (int i = 0; i < n; ++i)
{
cin>>a[i];
}sort(a,a+n);int flag=0;
for (int i = 2; i < n; ++i)
{
if(a[i-2]+a[i-1]>a[i]){
flag=1;break;
}
}if(flag)cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
C. Mahmoud and a Message
https://codeforces.com/contest/766/problem/C
题解
dp;
定义dp[i]为位置i的最大分割方案,minn[i]为位置i的最小分组数,maxn为最长字串的长度。
对于位置i前的分割长度j(如果合理)有:dp[i]=dp[i]+dp[i-j],minn[i]=min(minn[i],minn[i-j]+1);
如果dp[i-j]>0的话有:maxn=max(maxn,j);
Code
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
const int N= 1e5+7;
const ll mod=1e9+7;
int dp[N];
//dp[i]表示在位置i的前缀中的最大分割方案,
int minn[N],maxn;
//minn[i]表示最少的分组数
//maxn表示最长的字串长度
int cnt[30];
string s;
bool check(int i,int j){
int k=i-j+1;
for (int p = k; p <= i; ++p)
{
if(cnt[s[p-1]-'a']<j)return false;
}return true;
}
int main(){
IO;
int t=1;
//cin>>t;
while(t--){
int n;
cin>>n;
cin>>s;
dp[0]=1;
for (int i = 0; i < 26; ++i)
{
cin>>cnt[i];
}
for (int i = 1; i <= n; ++i)
{
minn[i]=inf;
for (int j = 1; j <= i; ++j)
{
if(check(i,j)){
dp[i]=(dp[i]+dp[i-j])%mod;
minn[i]=min(minn[i],minn[i-j]+1);
if(dp[i-j])
maxn=max(maxn,j);
}
}
}cout<<dp[n]<<endl<<maxn<<endl<<minn[n]<<endl;
}
return 0;
}
D. Mahmoud and a Dictionary
https://codeforces.com/contest/766/problem/D
题解
带权并查集的板子题。
在并查集的基础上,给每条边附上权值,0代表同义,1代表异义。
每次加边时判断两个点是否在同一个图中,如果不在则可以加,如果在判断两个点间权值是否与给定权值相同,若不同则无法加入次边。
最后询问时判断两个单词是否在同一个图中,若不在输出3,否则直接输出权值+1。
Code
#include<bits/stdc++.h>
#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
using namespace std;
const int inf=0x3f3f3f3f;
typedef long long ll;
const int N= 1e5+7;
const ll mod=998244353;
int fa[N],w[N];
int find(int x){
if(x!=fa[x]){
int t=fa[x];
fa[x]=find(fa[x]);
w[x]=(w[x]+w[t])%2;
}return fa[x];
}
int main(){
IO;
int t=1;
//cin>>t;
while(t--){
int n,m,q;
cin>>n>>m>>q;
vector<string>v;
map<string,int>mp;
for (int i = 1; i <= n; ++i)
{
string x;
cin>>x;
v.push_back(x);
mp[x]=i;
fa[i]=i;
w[i]=0;
}
for (int i = 0; i < m; ++i)
{
int z,x,y;
string s1,s2;
cin>>z>>s1>>s2;
z--;
x=mp[s1];
y=mp[s2];
int px=find(x),py=find(y);
if(px!=py){
cout<<"YES"<<endl;
fa[py]=px;
w[py]=(w[x]-w[y]+z+2)%2;
}else{
if((w[x]-w[y]+2)%2!=z){
cout<<"NO"<<endl;
}else cout<<"YES"<<endl;
}
}
while(q--){
string s1,s2;
cin>>s1>>s2;
int x=mp[s1],y=mp[s2];
int px=find(x),py=find(y);
if(px!=py)cout<<3<<endl;
else cout<<(w[x]-w[y]+2)%2+1<<endl;
}
}
return 0;
}
Code will change the world !
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 长文讲解 MCP 和案例实战
· Hangfire Redis 实现秒级定时任务,使用 CQRS 实现动态执行代码
· Android编译时动态插入代码原理与实践
· 使用TypeScript开发微信小程序(云开发)-入门篇
· 没几个人需要了解的JDK知识,我却花了3天时间研究
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 管理100个小程序-很难吗
· 在SqlSugar的开发框架中增加对低代码EAV模型(实体-属性-值)的WebAPI实现支持