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\n");
        else
            cout<<"NO"<<endl;
        //printf("NO\n");}
    }
    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\n");
        else
            cout<<"NO"<<endl;
        //printf("NO\n");}
    }
}

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值