zoukankan      html  css  js  c++  java
  • CF286-A

    A. Mr. Kitayuta's Gift
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Mr. Kitayuta has kindly given you a string s consisting of lowercase English letters. You are asked to insert exactly one lowercase English letter into s to make it a palindrome. A palindrome is a string that reads the same forward and backward. For example, "noon", "testset" and "a" are all palindromes, while "test" and "kitayuta" are not.

    You can choose any lowercase English letter, and insert it to any position of s, possibly to the beginning or the end of s. You have to insert a letter even if the given string is already a palindrome.

    If it is possible to insert one lowercase English letter into s so that the resulting string will be a palindrome, print the string after the insertion. Otherwise, print "NA" (without quotes, case-sensitive). In case there is more than one palindrome that can be obtained, you are allowed to print any of them.

    Input

    The only line of the input contains a string s (1 ≤ |s| ≤ 10). Each character in s is a lowercase English letter.

    Output

    If it is possible to turn s into a palindrome by inserting one lowercase English letter, print the resulting string in a single line. Otherwise, print "NA" (without quotes, case-sensitive). In case there is more than one solution, any of them will be accepted.

    Sample test(s)
    input
    revive
    output
    reviver
    input
    ee
    output
    eye
    input
    kitayuta
    output
    NA
    Note

    For the first sample, insert 'r' to the end of "revive" to obtain a palindrome "reviver".

    For the second sample, there is more than one solution. For example, "eve" will also be accepted.

    For the third sample, it is not possible to turn "kitayuta" into a palindrome by just inserting one letter.

    给出一个字符串,问是否能向其中插入一个字符使得新的字符串是个回文串

    由于给出的字符串长度很短,最大只有10.故可以暴力求解

    要使得插入字符后新字符串成为回文串,那么该字符必须是原字符串中的某一字符

    故枚举原字符串中的所有字符,并枚举插入的位置,得到一个新串,判断是否是回文串即可.

    import java.security.PublicKey;
    import java.util.Scanner;
    public class problem1
    {
        public static void main(String[] args)
        {
            String s;
            Scanner scanner=new Scanner(System.in);
            s=scanner.nextLine();
            boolean flag=false;
            for(int i=0;i<s.length();i++)
            {
                if(flag)
                    break;
                String str;
                for(int j=0;j<=s.length();j++)
                {
                    str=s.substring(0,j)+s.charAt(i)+s.substring(j,s.length());
                    if(judget(str)&&!flag)
                    {
                        System.out.println(str);
                        flag=true;
                    }
                }
            }
            if(!flag)
                System.out.println("NA");
        }
        public static boolean judget(String s)
        {
            for(int i=0;i<s.length();i++)
                if(s.charAt(i)!=s.charAt(s.length()-1-i))
                    return false;
            return true;
        }
    }
    
  • 相关阅读:
    帝国cms留言表模板修改
    帝国cms内容页模版
    百度竞价中的精确匹配,短语匹配,广泛匹配有什么区别。
    Android打包之多版本、多环境、多渠道
    Jenkins修改默认目录 /root/.jenkins
    Centos挂载硬盘完整图文教程(查看、分区、格式化、挂载)磁盘
    Linux安装mysql后systemctl start mysqld.service不能启动 修改datadir路径后不能启动
    Jenkins版本回滚
    Linux 调整时区与时间同步
    jenkins忘记admin密码的处理方法
  • 原文地址:https://www.cnblogs.com/wzsblogs/p/4288756.html
Copyright © 2011-2022 走看看