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;
        }
    }
    
  • 相关阅读:
    双向(端)链表、栈、队列
    WPF 3D基础(1)
    静态查找
    栈和队列 迷宫求解
    异步编程Demo
    WPF 3D基础(2)
    串操作
    链栈和链队
    Linux恢复数据
    word文件修复窍门
  • 原文地址:https://www.cnblogs.com/wzsblogs/p/4288756.html
Copyright © 2011-2022 走看看