Practice_Codeforces Round #410 (Div. 2)

来源:互联网 发布:大数据清洗工具 编辑:程序博客网 时间:2024/05/16 17:17

前言:由于太弱了,目前只水了前三道题。

注:第二三道参考(抄)了别人的题解。


A. Mike and palindrome
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome.

A palindrome is a string that reads the same backward as forward, for example strings "z", "aaa", "aba", "abccba" are palindromes, but strings "codeforces", "reality", "ab" are not.

Input

The first and single line contains string s (1 ≤ |s| ≤ 15).

Output

Print "YES" (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or "NO" (without quotes) otherwise.

Examples
input
abccaa
output
YES
input
abbcca
output
NO
input
abcda
output
YES


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


题意:给定一个字符串,修改其中的一个字符,看是否能变成回文串。比如abccaa中将第五个字符a改成b即可变成

回文串。

题解:对字符串进行“回文串”判断,对两边对称字符不相同的记数,如果为1,则说明改变一个字符即可变成字符串。

题坑:当然,要注意有坑,如果输入的字符串本来就是字符串,类似xxxxaxxxx这种形式改变最中间的字符仍然是回文串,如果是xxxxxx就不行了。

弱者的代码:

#include <iostream>#include <cstdio>using namespace std;bool IsPalin(string s){    int k = 0;    int l = s.length();    for(int i = 0, j = l - 1; i < l/2; i++, j--)        if(s[i] != s[j])            k++;    if(k == 1 || (l % 2 == 1 && k == 0))        return true;    return false;}int main(){    string s;    while (cin >> s)    {        if (IsPalin(s))            cout << "YES\n";        else            cout << "NO\n";    }    return 0;}

B. Mike and strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".

Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?

Input

The first line contains integer n (1 ≤ n ≤ 50) — the number of strings.

This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.

Output

Print the minimal number of moves Mike needs in order to make all the strings equal or print  - 1 if there is no solution.

Examples
input
4xzzwozwoxzzzwoxxzzwo
output
5
input
2molzvlzvmo
output
2
input
3kckckc
output
0
input
3aaaaab
output
-1
Note

In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".


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

题意:给定n个字符串, 进行循环左移操作,求使所有的字符串相同的最小操作数,比如molzv和lzvmo,只需将l和z左移即可完成。最小操作数为2.

题解:首先对输入的所有的字符串进行检测,如果字符串的字符不相同,则输出-1,否则进行枚举暴力计数,从第一个字符串开始,计算它后面的所有字符串左移后与它相同的操作数,然后更新,得到最小的操作数。

注:在观看他人题解的过程中,发现了更好的方法,暂不贴代码。

代码:

#include <iostream>#include <cstdio>#include <vector>#include <algorithm>using namespace std;bool check(string s1, string s2){    char c;    string s3 = s2;    do    {        c= s2[0];        s2.erase(s2.begin());        s2 += c;    }    while (s1 != s2 && s2 != s3);    return (s1 == s2);}int count(string s1, string s2){    int cnt = 0;    char c;    while (s1 != s2)    {        c = s2[0];        s2.erase(s2.begin());        s2 += c;        ++cnt;    }    return cnt;}int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int n, i, j;    vector <string> v;    string s, temp;    cin >> n;    for (i = 0; i < n; i++)    {        cin >> s;        v.push_back(s);    }    temp = v[0];    for (int i = 1; i < n; i++)        if(!check(temp, v[i]))        {            cout << "-1\n";            return 0;        }    int sum1 = (int)n*temp.size(), sum2 = 0;    for (i = 0; i < n; i++)    {        for (j = 0; j < n; j++)            if(i != j)                sum2 += count(v[i], v[j]);        sum1 = min(sum1, sum2);        sum2 = 0;    }    cout << sum1 << endl;}

学习新姿势

ios::sync_with_stdio(false);cin.tie(0);

#include <bits/stdc++.h>

C. Mike and gcd problem
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ..., bn] beautiful if the gcd of all its elements is bigger than 1, i.e. .

Mike wants to change his sequence in order to make it beautiful. In one move he can choose an index i (1 ≤ i < n), delete numbers ai, ai + 1 and put numbers ai - ai + 1, ai + ai + 1 in their place instead, in this order. He wants perform as few operations as possible. Find the minimal number of operations to make sequence A beautiful if it's possible, or tell him that it is impossible to do so.

 is the biggest non-negative number d such that d divides bi for every i (1 ≤ i ≤ n).

Input

The first line contains a single integer n (2 ≤ n ≤ 100 000) — length of sequence A.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 109) — elements of sequence A.

Output

Output on the first line "YES" (without quotes) if it is possible to make sequence A beautiful by performing operations described above, and "NO" (without quotes) otherwise.

If the answer was "YES", output the minimal number of moves needed to make sequence A beautiful.

Examples
input
21 1
output
YES1
input
36 2 4
output
YES0
input
21 3
output
YES1
Note

In the first example you can simply make one move to obtain sequence [0, 2] with .

In the second example the gcd of the sequence is already greater than 1.


题目链接:http://codeforces.com/contest/798/problem/C

题意:给定一个序列,问其中的相邻两元素 a [ i ] , a [ i + 1 ] 改为 a [ i ] - a [ i + 1 ] 和 a [ i ] + a [ i + 1 ]后能否使该序列(所有数字)的最大公约数大于1,如果可以,输出最小的操作数。

题解: 显然我们把所有奇数修改成偶数即可满足条件,每次修改时若  a [ i ] 和 a [ i + 1 ] 同为奇数则修改一次,若一奇一偶则修改两次。

代码:

#include <iostream>#include <cstring>#include <cstdio>using namespace std;const int maxn = 100000 + 10;int n;int a[maxn], b[maxn];int gcd(int a, int b){    int c;    while (b)    {        c = a % b;        a = b;        b = c;    }    return a;}int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int n;    cin >> n;    memset(b, 0, sizeof(b));    for (int i = 0; i < n; i++)        cin >> a[i];    int ans = 0, tmp = gcd(a[0], a[1]);    for (int i = 0; i < n; i++)    {        tmp = gcd(tmp, a[i]);        if (a[i] & 1 && !b[i])        {            if (a[i+1] & 1)                ans++;            else                ans += 2;            b[i] = 1;            b[i+1] = 1;        }    }    if (tmp != 1)        cout << "YES\n0\n";    else        cout << "YES\n" << ans << "\n";    return 0;}

The end.

0 0
原创粉丝点击