cf 410

A. Mike and palindrome
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
题意:改变一个字符,使它成为回文字符串
我忘记考虑字符个数为奇数且是回文字符串,那么中间那个可以改变
而偶数时不可以

#include<stdio.h>
#include<string>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    string s;
    cin>>s;
    int sum=0;
    int k=s.size();
    if(k%2==0)
    {
        int m=(k+1)/2-1;
        //int sum=0;
        int j=k-1;
        for(int i=0; i<=m; i++)
        {
            if(s[i]!=s[j--])
                sum++;
            //cout<<s[i]<<" "<<s[j]<<endl;
        }
        if(sum==1)
            cout<<"YES"<<endl;
        //printf("YES
");
        else
            cout<<"NO"<<endl;
        //printf("NO
");}
    }
    else
    {
        int m=(k+1)/2-1;
        //int sum=0;
        int j=k-1;
        for(int i=0; i<=m; i++)
        {
            if(s[i]!=s[j--])
                sum++;
            //cout<<s[i]<<" "<<s[j]<<endl;
        }
        if(sum==1||sum==0)
            cout<<"YES"<<endl;
        //printf("YES
");
        else
            cout<<"NO"<<endl;
        //printf("NO
");}
    }
}

B. Mike and strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
4
xzzwo
zwoxz
zzwox
xzzwo
output
5
input
2
molzv
lzvmo
output
2
input
3
kc
kc
kc
output
0
input
3
aa
aa
ab
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”.
题意:给你n个字符串,让你以其中一个为基准,使别的字符串与它相同(移动方法为把一个字符串首字母移到末尾)。
问最少移动次数是多少

#include <iostream>
#include <algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
int main()
{
    int n;
    cin>>n;
    string s[55];
    int sum,minn=inf;
    for(int i=1;i<=n;i++)
        cin>>s[i];
    for(int i=1;i<=n;i++)
    {
        sum=0;
        //cout<<i<<endl;
        for(int j=1;j<=n;j++)
        {
            if(i!=j)
            {
                //cout<<i<<" "<<j<<endl;
                //cout<<s[j]<<endl;
                string temp=s[j]+s[j];
                //cout<<temp<<endl;
                if(temp.find(s[i])==string::npos)//没找到相同字符串
                {
                    cout<<-1<<endl;
                    return 0;
                }
                //cout<<temp.find(s[i])<<endl;
                sum+=temp.find(s[i]);//直接找到下标
            }
        }
        if(sum<minn)
            minn=sum;
    }
    cout<<minn<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/zxy160/p/7215112.html