ABC 285 ABCD

https://atcoder.jp/contests/abc285/tasks

A - Edge Checker 2

题目大意:

二叉树,给定两个数字,问其中一个是否和另一个数字直接连线?也即是是否是父节点?
Sample Input 1  
1 2
Sample Output 1  
Yes
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=1000200,M=4002;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL x,y;
        cin>>x>>y;
        if(x==y/2||x/2==y) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

B - Longest Uncommon Prefix

题目大意:

给定长度为n的字符串S,从1到n,i为间隔个数,求S[k]!=S[k+i],一旦==立即停止.
Sample Input 1  
6
abcbac
Sample Output 1  
5
1
2
0
1
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=1000200,M=4002;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        LL n;
        cin>>n;
        string s;
        cin>>s;
        s=" "+s;
        for(int i=1;i<n;i++)
        {
            LL sum=0;
            for(int l=1,r=1+i;r<=n;l++,r++)
            {
                //cout<<s[l]<<" "<<s[r]<<endl;
                if(s[l]!=s[r]) sum++;
                else break;
            }
            cout<<sum<<endl;
        }
    }
    return 0;
}

C - abc285_brutmhyhiizp

题目大意:

A-z为1-26,AA27 AB28~ 给定一个字符串问表示的数字是多少?
Sample Input 3  
BRUTMHYHIIZP
Sample Output 3  
10000000000000000

从后往前依次计算,每一个字母根据位置所在都可以有26的i次方种搭配

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=1000200,M=4002;
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        string s;
        cin>>s;
        reverse(s.begin(),s.end());
        map<char,LL> mp;
        for(int i=65;i<=90;i++)
        {
            mp[(char)i]=i-64;
        }
        LL sum=0;
        for(int i=0;i<s.size();i++)
        {
            if(i==0) sum+=mp[s[i]];
            else sum+=(LL)pow(26,i)*(mp[s[i]]);
        }
        cout<<sum<<endl;
    }
    return 0;
}

D - Change Usernames

题目大意:

左边的网名想换成右边的网名,但是注意网名不能重复,问是否能够全部换成功?
Sample Input 3  
5
aaa bbb
yyy zzz
ccc ddd
xxx yyy
bbb ccc
Sample Output 3  
Yes

感觉我的方法繁琐了一些,用了离散化,然后分别存了前一个节点和下一个节点,还附带了状态值和个数
如果实在看不懂可以换下一个

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
const LL MAXN=1e18;
const LL N=1000200,M=4002;
LL beg=1,ans=0,a[N],b[N],st[N],last[N];
string s[N],c[N];
vector<LL> v[N];
bool flag=true;
void dfs(LL x)
{
    if(st[x]!=0||flag==false) return ;
    st[x]=1;
    ans--;
    for(int i=0;i<v[x].size();i++)
    {
        if(st[v[x][i]]==0) dfs(v[x][i]);
        else
        {
            flag=false; return ;
        }
    }
}
int main()
{
    cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
    LL T=1;
    //cin>>T;
    while(T--)
    {
        memset(last,0,sizeof last);
        LL n;
        cin>>n;
        map<string,LL> mp;
        for(int i=1;i<=n;i++)
        {
            cin>>s[i]>>c[i];
            if(mp[s[i]]==0) ans++,a[i]=beg++,mp[s[i]]=a[i];
            else a[i]=mp[s[i]];
            if(mp[c[i]]==0) ans++,b[i]=beg++,mp[c[i]]=b[i];
            else b[i]=mp[c[i]];
            //cout<<a[i]<<" "<<b[i]<<endl;
            v[a[i]].push_back(b[i]);
            last[b[i]]=a[i];
        }
        for(int i=1;i<=n;i++)
        {
            if(last[a[i]]==0)
            {
                dfs(a[i]);
                if(flag==false) break;
            }
        }
        if(ans!=0||flag==false) cout<<"No"<<endl;
        else cout<<"Yes"<<endl;
    }
    return 0;
}

原文链接: https://www.cnblogs.com/Vivian-0918/p/17054230.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    ABC 285 ABCD

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/312215

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月16日 下午12:19
下一篇 2023年2月16日 下午12:19

相关推荐