京京和东东是好朋友。东东很喜欢回文。回文是指从前往后读和从后往前读是一样的词语。京京准备给东东一个惊喜,先取定一个字符串s,然后在后面附上 0 个或者更多个字母形成回文,京京希望这个回文越短越好。请帮助京京计算他能够得到的最短的回文长度。
数据范围:输入的字符串长度满足 ,且保证只含有小写英文字母
输入包括一个字符串s,字符串s长度length
输出一个整数,表示牛牛能够得到的最短的回文长度。
abab
5
在末尾添加一个 'a' 构成回文
a
1
本身就是回文
import java.util.*; public class Main { private static final int MAX = 50+5; public static void main(String[] args) { Scanner sc = new Scanner(System.in); char[] arr = sc.next().toCharArray(); int n = arr.length; for (int i=0; i!=n; i++) { if (judgePlalindrome(arr, i, n-1)) { System.out.println(n+i); return; } } return; } private static boolean judgePlalindrome(char[]a, int i, int j) { while (i <= j) { if (a[i++] != a[j--]) { return false; } } return true; } }
import java.util.Scanner; public class Main { public static boolean isPalindrome(char[] ch, int i, int j) { while (i < j) { if (ch[i++] != ch[j--]) { return false; } } return true; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); char[] ch = sc.next().toCharArray(); for (int i = 0; i < ch.length; i++) { if (isPalindrome(ch, i, ch.length - 1)) { System.out.println(ch.length + i); break; } } } }
#include<iostream> #include<vector> #include<algorithm> #include<string> using namespace std; bool huiwen(string s) { string s2 = s; reverse(s2.begin(),s2.end()); if (s2 == s) return true; else return false; } int main() { string s; cin >> s; if(huiwen(s))//如果s本身就是回文就不需要再添加字母,直接输出s的长度就行 { cout<<s.size()<<endl; } else { int i=0; for (i = 1; i < s.size(); ++i) { string tmp = s; string tmp1 = tmp.substr(0,i); reverse(tmp1.begin(), tmp1.end()); tmp = tmp +tmp1 ; if (huiwen(tmp) == true) { cout << tmp.size() << endl; break; } } } return 0; }
//只能在末尾进行添加 #include <string> #include <iostream> using namespace std; bool judge(string word){ int lo=0,hi=(int)word.length()-1; while(lo<=hi) if(word[lo++]!=word[hi--]) return false; return true; } int main(){ string s; cin>>s; if(judge(s)){ //如果自身就是一个回文串 cout<<(int)s.length(); return 0; } //否则在尾部进行添加 int i; for(i=1;i<(int)s.length();++i){ if(s[i]==s.back()){ //找第一个可能的最小情况 if(judge(s.substr(i))){ //如果当前区间内已经是回文,添加i之前的字符即可 cout<<(int)s.length()+i; return 0; } } } cout<<2*(int)s.length(); return 0; }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine().trim(); int n = str.length(); int start = 0; while(start < n){ if(isPalindrome(str.substring(start, n))) break; start ++; } System.out.println(start + n); } private static boolean isPalindrome(String str){ int left = 0, right = str.length() - 1; while(left < right){ if(str.charAt(left) != str.charAt(right)) return false; else{ left ++; right --; } } return true; } }
//最能向后追加的最小长度回文字符串 mystr = input() n = len(mystr) if n==1: print(1) else: allflag = True //全部相同 obj = mystr[0] dp = [2*n-1]*n for index in range(1,n): if obj != mystr[index]: allflag = False if index < n - index - 1: continue else: flag1 = flag2 = True //奇偶 for i in range(index+1,n): if mystr[i]!=mystr[index-(i-index)]: flag1 = False if mystr[i]!=mystr[index-(i-index)+1]: flag2 = False mymin = 2*n-1 if flag1 and 2*index+1 <mymin: mymin = 2*index+1 if flag2 and 2*(index+1)<mymin: mymin = 2*(index+1) dp[index] = mymin if allflag: print(n) else: print(min(dp))
#include<iostream> #include<stdio.h> #include<algorithm> #include<cstring> using namespace std; int main(){ string s; cin >> s; int strSize = s.size(); if(strSize == 1){ cout << 1 << endl; return 0; } while(strSize < 2 * s.size() - 1){ if(strSize % 2 == 0){ //偶数 int m = strSize / 2; int n = m - 1; while(n >= 0 && m < s.size()){ if(s[n] == s[m]){ if(n == 0 || m == s.size() - 1){ cout << strSize << endl; return 0; } n--; m++; } else{ break; } } strSize++; } else{ //不为1的奇数 int m = strSize / 2 + 1; int n = strSize / 2 - 1; while(n >= 0 && m < s.size()){ if(s[n] == s[m]){ if(n == 0 || m == s.size() - 1){ cout << strSize << endl; return 0; } n--; m++; } else{ break; } } strSize++; } } cout << strSize << endl; return 0; }
#include<iostream> #include<sstream> using namespace std; bool Palindrome(string& str,int cur_idx) { int sz = str.size()- cur_idx; for(int i=0;i<sz/2;++i) { if(str[i+cur_idx]!=str[cur_idx+sz-i-1]) { return false; } } return true; } int main() { string str; while(cin>>str) { int sz_origin = str.size(); int i=0; // 判断[i,sz_origin]之间的字符是不是回文 // 最后的结果为 i+sz_origin while(!Palindrome(str,i)) { ++i; } cout<<i+sz_origin<<endl; } return 0; }
import java.util.*; public class Main{ public static void main(String[] args){ String s = new Scanner(System.in).nextLine(); for(int i=0;i<s.length();++i){ if(check(s.substring(i))){ System.out.println(i+s.length()); break; } } } public static boolean check(String x){ StringBuffer sb = new StringBuffer(x); return x.equals(sb.reverse().toString()); } }
#include <stdio.h> #include <string.h> char s[60]; int main() { int i,j,k,len,flag; gets(s); len = strlen(s); for(i=0;i<len;i++) { flag = 1; char ss[60]={0}; for(j=0;j<len;j++) ss[j] = s[j]; for(k=i-1;k>=0;k--) ss[j++] = s[k]; for(j--,k=0;j>=k;j--,k++) if(ss[j]!=ss[k]) { flag = 0; break; } if(flag) { printf("%d\n",len+i); return 0; } } return 0; }
//我说怎么似曾相识,又写了一遍 import java.util.*; public class Main{ public static void main(String[] arg){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ Cal(sc); } } public static void Cal(Scanner sc){ String s=sc.next(); int i=0; for(i=0;i<s.length();i++){ String t=s.substring(i); if(PanDu(t)) break; } System.out.println(s.length()+i); } public static boolean PanDu(String s){ StringBuffer t=new StringBuffer(s); t.reverse(); if(s.equals(t.toString())) return true; else return false; } } //********************************************************************* public static void main(String[] args){ Scanner sc=new Scanner(System.in); while(sc.hasNext()){ CAL(sc); } } public static void CAL(Scanner sc){ String s=sc.next(); String c=""; { int i=0; for (i=0;i<s.length()-1;i++){ c=c+s.charAt(i); c=c+'*'; } c=c+s.charAt(i); } int j=0; for(int i=c.length()/2;i<c.length();i++,j=j+2){ if (PanDu(c.substring(j,i),c.substring(i+1))){ break; } } System.out.println(j/2+s.length()); } public static boolean PanDu(String s1,String s2){ StringBuffer cBuffer2=new StringBuffer(s2); s2=cBuffer2.reverse().toString(); return s1.equals(s2); }