Practice_Codeforces Round #410 (Div. 2)
来源:互联网 发布:大数据清洗工具 编辑:程序博客网 时间:2024/05/16 17:17
前言:由于太弱了,目前只水了前三道题。
注:第二三道参考(抄)了别人的题解。
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.
The first and single line contains string s (1 ≤ |s| ≤ 15).
Print "YES" (without quotes) if Mike can change exactly one character so that the resulting string is palindrome or "NO" (without quotes) otherwise.
abccaa
YES
abbcca
NO
abcda
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;}
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?
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.
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
4xzzwozwoxzzzwoxxzzwo
5
2molzvlzvmo
2
3kckckc
0
3aaaaab
-1
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>
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).
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 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.
21 1
YES1
36 2 4
YES0
21 3
YES1
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.
- Practice_Codeforces Round #410 (Div. 2)
- Practice_Codeforces Round #409 (Div. 2)
- Practice_Codeforces Round #408 (Div. 2)
- Practice_Codeforces Round #407 (Div. 2)
- Practice_Codeforces Round #406 (Div. 2)
- Practice_Codeforces Round #405 (Div. 2)
- Practice_Codeforces Round #404 (Div. 2)
- Practice_Codeforces Round #411 (Div. 2)
- Practice_Codeforces Round #412 (Div. 2)
- Practice_Codeforces Round #413 (Div. 2)
- Codeforces Round #410 (Div. 2)
- Codeforces Round #410 (Div. 2)
- Codeforces Round #410 (Div. 2)
- Codeforces Round #410 (Div. 2)
- 【解题报告】Codeforces Round #410 (Div. 2)
- Codeforces Round #410 (Div. 2) 部分解析
- 【Codeforces Round #410 (Div. 2) B】暴力
- Codeforces Round #410 (Div. 2) 解题报告
- PHP Filesystem 函数---文件操作函数
- HDU5551-Huatuo's Medicine
- UE4 渲染性能设置
- PHP程序开发之使用header定义一个php页面为utf-8编码或GBK编码
- mysql中date、time、datetime、timestamp区别
- Practice_Codeforces Round #410 (Div. 2)
- CSS盒子模型
- DNS域名解析
- NJU 1010 Air
- 发现JDK下好用的几个工具(内存监视)
- VS2017下OpenCV3.2学习笔记(一)安装配置、测试程序
- SQLite实现数据持久化存储小案例之购买商品
- C++ Boost::bind函数包装器使用,boost::bind与伪函数的绑定使用
- 封装RecycleView的Adapter并实现列表展示