Codeforces Round #620 (Div. 2)

A. Two Rabbits

Being tired of participating in too many Codeforces rounds, Gildong decided to take some rest in a park. He sat down on a bench, and soon he found two rabbits hopping around. One of the rabbits was taller than the other.

He noticed that the two rabbits were hopping towards each other. The positions of the two rabbits can be represented as integer coordinates on a horizontal line. The taller rabbit is currently on position xx, and the shorter rabbit is currently on position yy (x<yx<y). Every second, each rabbit hops to another position. The taller rabbit hops to the positive direction by aa, and the shorter rabbit hops to the negative direction by bb.

For example, let’s say x=0x=0, y=10y=10, a=2a=2, and b=3b=3. At the 11-st second, each rabbit will be at position 22 and 77. At the 22-nd second, both rabbits will be at position 44.

Gildong is now wondering: Will the two rabbits be at the same position at the same moment? If so, how long will it take? Let’s find a moment in time (in seconds) after which the rabbits will be at the same point.

Input
Each test contains one or more test cases. The first line contains the number of test cases tt (1≤t≤10001≤t≤1000).

Each test case contains exactly one line. The line consists of four integers xx, yy, aa, bb (0≤x<y≤1090≤x<y≤109, 1≤a,b≤1091≤a,b≤109) — the current position of the taller rabbit, the current position of the shorter rabbit, the hopping distance of the taller rabbit, and the hopping distance of the shorter rabbit, respectively.

Output
For each test case, print the single integer: number of seconds the two rabbits will take to be at the same position.

If the two rabbits will never be at the same position simultaneously, print −1−1.

Example

inputCopy
5
0 10 2 3
0 10 3 3
900000000 1000000000 1 9999999
1 2 1 1
1 3 1 1
outputCopy
2
-1
10
-1
1
Note
The first case is explained in the description.

In the second case, each rabbit will be at position 33 and 77 respectively at the 11-st second. But in the 22-nd second they will be at 66 and 44 respectively, and we can see that they will never be at the same position since the distance between the two rabbits will only increase afterward.

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    int t;
    cin>>t;
    long long int x,y,a,b;
    while(t--){
        scanf("%lld%lld%lld%lld",&x,&y,&a,&b);
        long long int l=y-x;
        if(l%(a+b)==0){
            printf("%d\n",l/(a+b));
        }
        else{
            printf("-1\n");
        }
    }
    return 0;
}

B. Longest Palindrome

Returning back to problem solving, Gildong is now studying about palindromes. He learned that a palindrome is a string that is the same as its reverse. For example, strings “pop”, “noon”, “x”, and “kkkkkk” are palindromes, while strings “moon”, “tv”, and “abab” are not. An empty string is also a palindrome.

Gildong loves this concept so much, so he wants to play with it. He has nn distinct strings of equal length mm. He wants to discard some of the strings (possibly none or all) and reorder the remaining strings so that the concatenation becomes a palindrome. He also wants the palindrome to be as long as possible. Please help him find one.

Input
The first line contains two integers nn and mm (1≤n≤1001≤n≤100, 1≤m≤501≤m≤50) — the number of strings and the length of each string.

Next nn lines contain a string of length mm each, consisting of lowercase Latin letters only. All strings are distinct.

Output
In the first line, print the length of the longest palindrome string you made.

In the second line, print that palindrome. If there are multiple answers, print any one of them. If the palindrome is empty, print an empty line or don’t print this line at all.

Examples

inputCopy
3 3
tab
one
bat
outputCopy
6
tabbat
inputCopy
4 2
oo
ox
xo
xx
outputCopy
6
oxxxxo
inputCopy
3 5
hello
codef
orces
outputCopy
0

inputCopy
9 4
abab
baba
abcd
bcde
cdef
defg
wxyz
zyxw
ijji
outputCopy
20
ababwxyzijjizyxwbaba
Note
In the first example, “battab” is also a valid answer.

In the second example, there can be 4 different valid answers including the sample output. We are not going to provide any hints for what the others are.

In the third example, the empty string is the only valid palindrome string.
题目大意:给出n条长为m的字符串,可以去除若干条,组成一条回文字符串(如tabbat),输出回文字符串的长度和字符串本身

解题思路:因为是组成回文字符串ans,所以首先判断是否有自我回文的字符串p,将其放在中间位置,然后依次遍历判断是否有字符串s的倒序t与别的字符串相等,若有将其存入ans中,最后只存入了一半的字符串,将字符串ans倒序存入t中,得到另一半,最后ans=ans+p+t,得到完整的回文字符串;

#include <iostream>
#include <set>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
    set <string> str;                                   ///储存字符串
    int n,m;
    cin>>n>>m;
    string s,t,p="",ans="";                             /// t存字符串倒序,p存自我回文的字符串
    for(int i=0;i<n;i++){
        cin>>s;
        t=s;
        reverse(t.begin(),t.end());                     ///颠倒函数:reverse()函数可以对字符串进行反转操作
        if(t==s){                                       ///判断条件1:找到自我回文字符串
            p=t;
        }
        else if(str.count(t)){                          ///判断条件2:判断是否有相互对称的字符串(只存一条)
            ans+=t;
        }
        str.insert(s);
    }
    t=ans;
    reverse(t.begin(),t.end());                         ///颠倒ans,获得另一半对称的字符串
    ans=ans+p+t;                                       
    cout<<ans.size()<<endl;
    cout<<ans<<endl;
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

向岸看

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

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

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

打赏作者

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

抵扣说明:

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

余额充值