Codeforces Round #409 Div. 2(A+B)

A. Vicious Keyboard
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Tonio has a keyboard with only two letters, “V” and “K”.

One day, he has typed out a string s with only these two letters. He really likes it when the string “VK” appears, so he wishes to change at most one letter in the string (or do no changes) to maximize the number of occurrences of that string. Compute the maximum number of times “VK” can appear as a substring (i. e. a letter “K” right after a letter “V”) in the resulting string.

Input
The first line will contain a string s consisting only of uppercase English letters “V” and “K” with length not less than 1 and not greater than 100.

Output
Output a single integer, the maximum number of times “VK” can appear as a substring of the given string after changing at most one character.

Examples
input
VK
output
1
input
VV
output
1
input
V
output
0
input
VKKKKKKKKKVVVVVVVVVK
output
3
input
KVKV
output
1
Note
For the first case, we do not change any letters. “VK” appears once, which is the maximum number of times it could appear.

For the second case, we can change the second character from a “V” to a “K”. This will give us the string “VK”. This has one occurrence of the string “VK” as a substring.

For the fourth case, we can change the fourth character from a “K” to a “V”. This will give us the string “VKKVKKKKKKVVVVVVVVVK”. This has three occurrences of the string “VK” as a substring. We can check no other moves can give us strictly more occurrences.
题意:给出一个仅由VK组成的字符串,你可以将其中一个V变成K或者K变成V,或者不变。求VK出现的次数。
题解:先看给出的串中有多少VK,然后判断有没连续的VV或者KK,有,答案+1.
代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<cmath>
#define ll long long
using namespace std;
char a[110];
int main()
{
    cin>>a;
    int len=strlen(a);
    int ans=0;
    for(int i=0;i<len;i++)
    {
        if(a[i]=='V'&&a[i+1]=='K')
        {
            ans++,a[i]='a',a[i+1]='a';
        }
    }
    int flag=0;
    for(int i=0;i<len;i++)
    {
        if((a[i]=='V'&&a[i+1]=='V')||(a[i]=='K'&&a[i+1]=='K'))
             flag=1;
    }
    cout<<ans+flag<<endl;

}

B. Valued Keys
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You found a mysterious function f. The function takes two strings s1 and s2. These strings must consist only of lowercase English letters, and must be the same length.

The output of the function f is another string of the same length. The i-th character of the output is equal to the minimum of the i-th character of s1 and the i-th character of s2.

For example, f(“ab”, “ba”) = “aa”, and f(“nzwzl”, “zizez”) = “niwel”.

You found two strings x and y of the same length and consisting of only lowercase English letters. Find any string z such that f(x, z) = y, or print -1 if no such string z exists.

Input
The first line of input contains the string x.

The second line of input contains the string y.

Both x and y consist only of lowercase English letters, x and y have same length and this length is between 1 and 100.

Output
If there is no string z such that f(x, z) = y, print -1.

Otherwise, print a string z such that f(x, z) = y. If there are multiple possible answers, print any of them. The string z should be the same length as x and y and consist only of lowercase English letters.

Examples
input
ab
aa
output
ba
input
nzwzl
niwel
output
xiyez
input
ab
ba
output
-1
Note
The first case is from the statement.

Another solution for the second case is “zizez”

There is no solution for the third case. That is, there is no z such that f(“ab”, z) =  “ba”.
题意:给出一种变换,F(x,y)=z. zi为min(xi,yi)。现在给出x,z。问是否有满足题意的y。无解输出-1.
题解:显然如果zi>xi,肯定无解。然后判断zi和xi的大小关系。如果xi>zi,则yi=zi。如果xi==zi,则yi>=xi即可(直接赋值‘z’).
代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<cmath>
#define ll long long
using namespace std;
char a[110];
char b[110];
int main()
{
    cin>>a>>b;
    int x=strlen(a);
    bool flag=true;
    for(int i=0;i<x;i++)
    {
        if(b[i]>a[i]) flag=false;
    }
    if(!flag)
        cout<<"-1"<<endl;
    else
    {
        for(int i=0;i<x;i++)
        {
            if(a[i]>b[i])
                cout<<b[i];
            else if(a[i]==b[i])
                cout<<'z';
        }
        cout<<endl;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值