Codeforces Round #361 (Div. 2) AB

A.Mike and Cellphone

题目大意:

Input

输入的第一行包含唯一的整数 n (1 ≤ n ≤ 9) —麦克输入的电话号码的位数。

第二行包含一个长度为 n 的字符串表示麦克输入的电话号码。

Output   

如果没有其他手机号码与手机相同,Mike可以肯定他正在拨打正确的号码,输出 "YES" (不包含引号)。 否则输出 "NO" 。

Input

3
586

Output

NO

Input

2
09

Output

NO

Input

9
123456789

Output

YES

Input

3
911

Output

YES

思路:用肌肉记忆敲老爷机,分上下左右两对角线的顶点六个方位,注意0的特判即可。

strstr的忠实爱好者。

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    int n;
    cin>>n;
    char s[10];
    for(int i=0;i<n;i++)
        cin>>s[i];
    //上下左右
    if(strstr(s,"1")!=NULL&&strstr(s,"9")!=NULL) cout<<"YES"<<endl;
    else if(strstr(s,"3")!=NULL&&strstr(s,"7")!=NULL) cout<<"YES"<<endl;
    else
    {
        int flag=0;
        if(strstr(s,"1")==NULL&&strstr(s,"2")==NULL&&strstr(s,"3")==NULL)//可以往上
        flag=1;
        if(strstr(s,"7")==NULL&&strstr(s,"0")==NULL&&strstr(s,"9")==NULL)//可以往下
        flag=1;
        if(strstr(s,"1")==NULL&&strstr(s,"4")==NULL&&strstr(s,"7")==NULL)//可以往左
        {
            if(strstr(s,"0")==NULL) flag=1;
            //else flag=1;
        }
        if(strstr(s,"3")==NULL&&strstr(s,"6")==NULL&&strstr(s,"9")==NULL)//可以往右
        {
            if(strstr(s,"0")==NULL) flag=1;
            //else flag=1;
        }
        if(flag==0) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

B. Mike and Shortcuts

题目大意:

Input

3
2 2 3

Output

0 1 2 

Input

5
1 2 3 4 5

Output

0 1 2 3 4 

Input

7
4 4 4 4 7 7 7

Output

0 1 2 1 2 3 3 

继周测wa了之后,cf又wa,才明白过来,意思get错了!我人麻了呜呜呜

学习一下学习一下

正确思路:bfs,捷径直达为1,每一个点到它的上一个点和下一个点步数也都为1。

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    queue<int> q;
    int a[200200],dis[200200];
    memset(dis,0x3f,sizeof dis);
    for(int i=1;i<=n;i++)
        cin>>a[i];
    q.push(1);
    dis[1]=0;
    while(q.size())
    {
        int t=q.front();
        q.pop();
        if(dis[a[t]]>dis[t]+1)//运用题目条件更新距离
        {
            dis[a[t]]=dis[t]+1; q.push(a[t]);
        }
        if(t<n&&dis[t+1]>dis[t]+1)//考虑下一位
        {
            dis[t+1]=dis[t]+1; q.push(t+1);
        }
        if(t>1&&dis[t-1]>dis[t]+1)//考虑上一位
        {
            dis[t-1]=dis[t]+1; q.push(t-1);
        }
    }
    for(int i=1;i<=n;i++)
        cout<<dis[i]<<" ";
    cout<<endl;
    return 0;
}

 一天的水课......乏了

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Vijurria

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

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

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

打赏作者

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

抵扣说明:

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

余额充值