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
题意:
改变一个字符,让这组字符串变成回文序列,能的话输出YES,不能输出NO
分析:
这道题有点坑,应该是卡了三组数据,aa输出NO,a输出YES,aba也输出YES,估计过了这三组,就可以AC了吧
这题我的做法是分奇偶性,判断条件s[i]==s[len-i-1]是否成立,成立的话ans++,然后判断奇数项长度ans==(len)/2-1||ans==len/2是否成立,成立输出YES,否则输出NO
偶数项长度只需判断ans==(len)/2-1是否成立,成立为YES,否则为NO
下面给出AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() 4 { 5 char s[20]; 6 while(cin>>s) 7 { 8 int len=strlen(s); 9 for(int i=0;i<len;i++) 10 { 11 for(int j='a';j<='z';j++) 12 { 13 if(s[i]==j) 14 continue; 15 } 16 } 17 int ans=0; 18 if(len%2!=0) 19 { 20 for(int i=0;i<(len)/2;i++) 21 { 22 if(s[i]==s[len-i-1]) 23 ans++; 24 } 25 if(ans==(len)/2-1||ans==len/2) 26 cout<<"YES"<<endl; 27 else cout<<"NO"<<endl; 28 29 } 30 else if(len%2==0) 31 { 32 for(int i=0;i<(len)/2;i++) 33 { 34 if(s[i]==s[len-i-1]) 35 ans++; 36 } 37 if(ans==(len)/2-1) 38 cout<<"YES"<<endl; 39 else cout<<"NO"<<endl; 40 } 41 } 42 return 0; 43 }
B. Mike and strings
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.
4
xzzwo
zwoxz
zzwox
xzzwo
5
2
molzv
lzvmo
2
3
kc
kc
kc
0
3
aa
aa
ab
-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
题意:
枚举一个字符串为标准,求答案,找最小
分析:
调用一个substr函数,具体函数详解请参看http://www.cnblogs.com/ECJTUACM-873284962/p/6763801.html
下面给出AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 void change(string &s) 4 { 5 s=s.substr(1)+s.substr(0,1);//字符串截取函数,这里的目的就是一个一个换字符,找最小次数 6 } 7 int main() 8 { 9 int n; 10 while(cin>>n) 11 { 12 string s[n]; 13 for(int i=0;i<n;i++) 14 cin>>s[i]; 15 int ans=1e+8; 16 int sum; 17 for(int i=0;i<n;i++) 18 { 19 sum=0; 20 for(int j=0;j<n;j++) 21 { 22 string t=s[j]; 23 while(t!=s[i]) 24 { 25 change(t); 26 sum++; 27 if(sum>3000) 28 { 29 ans=-1; 30 break; 31 } 32 } 33 } 34 ans=min(ans,sum); 35 } 36 cout<<ans<<endl; 37 } 38 return 0; 39 }
C. Mike and gcd problem
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.
2
1 1
YES
1
3
6 2 4
YES
0
2
1 3
YES
1
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.