leetcode每日一题(5.7)+Codeforces Round #788 (Div. 2)AB

42 篇文章 2 订阅
35 篇文章 3 订阅

LeetCode每日一题

433. 最小基因变化

hash+bfs

class Solution {
public:
    int minMutation(string start, string end, vector<string>& bank) {
        unordered_set<string>cnt;
        unordered_map<string,bool>st;
        unordered_map<string,int>dist;
        char word[4]={'A','C','G','T'};
        for(auto x:bank)cnt.insert(x);
        if(start==end)return 0;
        if(!cnt.count(end))return -1;
        queue<string>q;
        q.push(start);
        dist[start]=0;
        st[start]=true;
        while(q.size())
        {
            auto t=q.front();
            q.pop();
            for(int i=0;i<8;i++)
            {
                for(int j=0;j<4;j++)
                {
                    if(t[i]!=word[j])
                    {
                        string next=t;
                        next[i]=word[j];
                        if(st[next]==false&&cnt.count(next))
                        {
                            dist[next]=dist[t]+1;
                            if(next==end)return dist[end];
                            q.push(next);
                            st[next]=true;
                        }
                    }
                }
            }
        }
        return -1;
    }
};

Codeforces

A.Prof. Slim

#include <bits/stdc++.h>
using namespace std;
int _;
const int N=1e5+10;
int a[N];
int n;
void solve()
{
	cin>>n;
	int cnt=0;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		if(a[i]<0)
		{
			cnt++;
			a[i]=-a[i];
		}
	}
	for(int i=0;i<cnt;i++)a[i]=-a[i];
	if(is_sorted(a,a+n))puts("YES");
	else puts("NO");
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
} 

B - Dorms War

#include <bits/stdc++.h>
using namespace std;
int _;
int n;
void solve()
{
	unordered_set<char>cnt;
	string s;
	int n;
	cin>>n;
	cin>>s;
	int k;
	cin>>k;
	for(int i=0;i<k;i++)
	{
		char c;
		cin>>c;
		cnt.insert(c);
	}
	int c=0;
	int res=0;
	for(int i=0;i<n;i++)
	{
		if(cnt.count(s[i]))
		{
			res=max(res,c);
			c=1;
		}
		else c++;
	}
	cout<<res<<endl;
}
int main()
{
	ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
 
	cin>>_;
	while(_--)solve();	
	return 0;
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

leimingzeOuO

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值