Codeforces——Hello 2019(待更新)

A. Gennady and a Card Game

Gennady owns a small hotel in the countryside where he lives a peaceful life. He loves to take long walks, watch sunsets and play cards with tourists staying in his hotel. His favorite game is called "Mau-Mau".

To play Mau-Mau, you need a pack of 52 cards. Each card has a suit (Diamonds — D, Clubs — C, Spades — S, or Hearts — H), and a rank (2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, or A).

At the start of the game, there is one card on the table and you have five cards in your hand. You can play a card from your hand if and only if it has the same rank or the same suit as the card on the table.

In order to check if you'd be a good playing partner, Gennady has prepared a task for you. Given the card on the table and five cards in your hand, check if you can play at least one card.

Input

The first line of the input contains one string which describes the card on the table. The second line contains five strings which describe the cards in your hand.

Each string is two characters long. The first character denotes the rank and belongs to the set {2,3,4,5,6,7,8,9,T,J,Q,K,A}. The second character denotes the suit and belongs to the set {D,C,S,H}.

All the cards in the input are different.

Output

If it is possible to play a card from your hand, print one word "YES". Otherwise, print "NO".

You can print each letter in any case (upper or lower).

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int main()
{
    string s,t[5];
    bool a=false;
    cin>>s;
    for(int i=0;i<5;i++)
    {
        cin>>t[i];
        if(s[0]==t[i][0]||s[1]==t[i][1])
        {
            a=true;
            break;
        }
    }
    if(a) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

B. Petr and a Combination Lock

Petr has just bought a new car. He's just arrived at the most known Petersburg's petrol station to refuel it when he suddenly discovered that the petrol tank is secured with a combination lock! The lock has a scale of 360 degrees and a pointer which initially points at zero:

Petr called his car dealer, who instructed him to rotate the lock's wheel exactly n times. The i-th rotation should be ai degrees, either clockwise or counterclockwise, and after all nn rotations the pointer should again point at zero.

This confused Petr a little bit as he isn't sure which rotations should be done clockwise and which should be done counterclockwise. As there are many possible ways of rotating the lock, help him and find out whether there exists at least one, such that after all nn rotations the pointer will point at zero again.

Input

The first line contains one integer n (1≤n≤15) — the number of rotations.

Each of the following n lines contains one integer ai (1≤ai≤180) — the angle of the i-th rotation in degrees.

Output

If it is possible to do all the rotations so that the pointer will point at zero after all of them are performed, print a single word "YES". Otherwise, print "NO". Petr will probably buy a new car in this case.

You can print each letter in any case (upper or lower).

分析:累加或累减的dfs

代码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int n,a[20];
int main()
{
    bool dfs(int x,int y);
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>a[i];
    if(dfs(0,0)) cout<<"YES"<<endl;
    else cout<<"NO"<<endl;
    return 0;
}

bool dfs(int x,int y)
{
    if(x==n) return !(y%360);
    if(dfs(x+1,y+a[x])) return true;
    if(dfs(x+1,y-a[x])) return true;
    return false;
}

C. Yuhao and a Parenthesis

One day, Yuhao came across a problem about checking if some bracket sequences are correct bracket sequences.

A bracket sequence is any non-empty sequence of opening and closing parentheses. A bracket sequence is called a correct bracket sequence if it's possible to obtain a correct arithmetic expression by inserting characters "+" and "1" into this sequence. For example, the sequences "(())()", "()" and "(()(()))" are correct, while the bracket sequences ")(", "(()" and "(()))(" are not correct.

Yuhao found this problem too simple for him so he decided to make the problem harder. You are given many (not necessarily correct) bracket sequences. The task is to connect some of them into ordered pairs so that each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence. The goal is to create as many pairs as possible.

This problem unfortunately turned out to be too difficult for Yuhao. Can you help him and solve it?

Input

The first line contains one integer n (1≤n≤105) — the number of bracket sequences.

Each of the following n lines contains one bracket sequence — a non-empty string which consists only of characters "(" and ")".

The sum of lengths of all bracket sequences in the input is at most 5⋅10^5.

Note that a bracket sequence may appear in the input multiple times. In this case, you can use each copy of the sequence separately. Also note that the order in which strings appear in the input doesn't matter.

Output

Print a single integer — the maximum number of pairs which can be made, adhering to the conditions in the statement.

分析:

l :无左括号配对的右括号数量    r :无右括号配对的左括号数量

只有当 l=0 或者 r=0 时,括号序列有正确配对的可能,进行存储

自我检讨:

(又一次因为英语理解能力差漏信息。。。又一次哭唧唧)

1.The task is to connect some of them into ordered pair

有序对,所以括号序列两两配对(不是多组配对)

2.each bracket sequence occurs in at most one pair and the concatenation of the bracket sequences in each pair is a correct bracket sequence.

每个括号序列只能使用一次

代码:

#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5*5+5;
int f[2*maxn+1]={0};
int main()
{
    int t,l,r,ans=0;
    string s;
    cin>>t;
    for(int i=0;i<t;i++)
    {
        s.clear();
        r=l=0;
        cin>>s;
        for(int j=0;j<s.size();j++)
        {
            if(s[j]=='(') r++;
            else if(s[j]==')')
                r>0?r--:l--;
        }
        if(l==0) f[maxn+r]++;
        else if(r==0) f[maxn+l]++;
    }
    ans=f[maxn]/2;
    for(int i=1;i<=maxn;i++)
    {
        ans+=min(f[maxn+i],f[maxn-i]);
    }
    cout<<ans<<endl;
    return 0;
}

 

 

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值