Practice_Codeforces Round #409 (Div. 2)

来源:互联网 发布:删除表的sql语句 编辑:程序博客网 时间:2024/05/16 16:04

蒟蒻的水题之路

根据外国友人的建议,有效的做题。

A. Vicious Keyboard
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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.


题目链接:http://codeforces.com/contest/801/problem/A

题意:给定一个字符串,问改变其中一个字符或者不改变后,能得到的子串VK的最大数量。
题解:首先统计字符串中子串VK的数量,然后判断是否符合改变字符后形成VK的条件,如果存在则结束加1即可。
可能存在的问题:VKKVVK,要求前两个为VV且第三个不为K.VKKKKK要求第一个不为V且后两个为K.即要求连续的三个相同的字符串。开头结尾可特判。
弱者的代码:
//VVKKVKKVVKKVKKVKVVKKVKKVVKKVKVVKKVKKVKVVKKVVKKVKVVKKVKVVKKVVKVVKKVKKVKKVKKVKKVKVVKKVKKVKKVKKVKKVVKVK#include <iostream>using namespace std;int main(){    ios::sync_with_stdio(false);    cin.tie(0);    string s;    cin >> s;    int cnt = 0;    if (s.length() == 1)    {        cout << "0\n";        return 0;    }    int l = s.length();    for (int i = 0; i < l - 1; i++)        if(s[i] == 'V' && s[i+1] == 'K')           cnt++;    if((s[0] == 'K' && s[1] == 'K'))        cnt++;    else if (s[l-1] == 'V' && s[l-2] == 'V')        cnt++;    else    {        for (int i = 0; i < l - 2; i++)        {            if(s[i] == s[i+1] && s[i] == s[i+2])            {                cnt++;                break;            }        }    }    cout << cnt << endl;    return 0;}

B. Valued Keys
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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
abaa
output
ba
input
nzwzlniwel
output
xiyez
input
abba
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".


题目链接:http://codeforces.com/contest/801/problem/B

题意:函数 f 指输出X和Y字符串中对应位置最小的字符形成的字符串Z。比如 f("nzwzl", "zizez") = "niwel".
现在给定X和Z求一个Y字符串。
题解:首先判断对应Y中的字符是否小于X,如果大于则不存在。然后如果对应的字符相同则找一个大于它们的,如果不同,则保存Y中的。
应该是大于或等于!!!事实上只要输出Y即可,我太菜了。

#include <iostream>using namespace std;int main(){    ios::sync_with_stdio(false);    cin.tie(0);    string x, y;    cin >> x >> y;    int l = x.length();    for (int i = 0; i < l; i++)    if (y[i] > x[i])    {        cout << "-1\n";        return 0;    }    cout << y << endl;    return 0;}

The end.

0 0
原创粉丝点击