每组输入数据共两行。 第一行为字符串A 第二行为字符串B 字符串长度均小于100且只包含小写字母
输出一个数字,表示把字符串B插入字符串A之后构成一个回文串的方法数
aba b
2
}
//没有拼接,而是两个指针分别从头尾0,m+n-1,根据插入位置计算分别是哪个字符 //效率上应该会快点,因为在头尾就不符合就立即退出了 import java.util.*; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while(input.hasNext()){ String a = input.next(); String b = input.next(); int n = a.length(),m=b.length(); int ans = 0; for(int i=0;i<=n;i++){//insert pos int p=0,q=m+n-1; while(p<=q){ if(get(p,i,a,b)==get(q,i,a,b)){ p++;q--; }else break; } if(p>q) ans++; } System.out.println(ans); } } public static char get(int index,int insert,String a,String b){ if(index<insert){ return a.charAt(index); }else if(index>=insert&&index<insert+b.length()){ return b.charAt(index-insert); }else{ return a.charAt(index-b.length()); } } }
import java.util.*; public class Main{ public static boolean isHuiWen(String str){ int i=0; int j=str.length()-1; while(i<j){ if(str.charAt(i)!=str.charAt(j)){ return false; } i++; j--; } return true; } public static int number(String a,String b){ int count=0; int len=a.length(); for(int i=0;i<=len;i++){ String str=a.substring(0,i)+b+a.substring(i,len); if(isHuiWen(str)){ count++; } } return count; } public static void main(String[] args){ Scanner s=new Scanner(System.in); while(s.hasNext()){ String a=s.next(); String b=s.next(); System.out.println(number(a,b)); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String str1 = sc.nextLine(); String str2 = sc.nextLine(); int len = str1.length(); int n = 0; for (int i = 0; i <= len; i++) { String str4 = str1.substring(0, i) + str2 + str1.substring(i); String str5 = new StringBuffer(str4).reverse().toString(); if (str4.equals(str5)) { n++; } } System.out.println(n); } } }
最喜欢这种题了 import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String a = scanner.nextLine(); String b = scanner.nextLine(); System.out.println(insert(a, b)); } public static int insert(String a, String b) { int cnt = 0; StringBuilder sb = new StringBuilder(); for (int i = 0;i <= a.length();i ++) { if (i == 0) { }else { sb.append(a.substring(0, i)); } sb.append(b); if (i == a.length()) { }else { sb.append(a.substring(i, a.length())); } if (legal(sb.toString())) { cnt ++; } sb = new StringBuilder(); } return cnt; } public static boolean legal(String s) { int i = 0, j = s.length() - 1; while (i < j) { if (s.charAt(i) != s.charAt(j)) { return false; } i ++; j --; } return true; } }
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
//System.out.print("请输入第一个字符串:");
String s1 = sc.nextLine();
//System.out.print("请输入第二个字符串:");
String s2 = sc.nextLine();
//获取第一行字符串的长度
int length = s1.length();
//计数
int count=0;
for(int i=0;i<=length;i++){
StringBuffer sb = new StringBuffer();
StringBuffer sb2 = new StringBuffer();
sb=sb.append(s1).insert(i,s2);
sb2=sb2.append(s1).insert(i,s2).reverse();
String s3=sb.toString();
String s4=sb2.toString();
//System.out.println(s3+"/"+s4+"/"+s3.equals(s4));
if(s3.equals(s4)){
count++;
}
}
System.out.print(count);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int num = 0;
String strA = sc.nextLine();
String strB = sc.nextLine();
for (int i = 0; i <= strA.length(); i++) {
StringBuffer strC=new StringBuffer(strA);
strC.insert(i, strB);
if ((strC.toString()).equals(strC.reverse().toString())) {
num++;
}
}
System.out.println(num);
}
sc.close();
}
}
#include <iostream> #include <string> using namespace std; bool isSymmetry(string s){ int n=s.size(); if(n==1){ return true; }else{ for(int i=0,j=n-1;i<j;i++,j--){ if(s[i]!=s[j]){ return false; } } return true; } } int main(){ int cnt=0; string A,B,temp; cin>>A>>B; for(int i=0;i<A.size()+1;i++){ temp=A.substr(0,i)+B+A.substr(i); if(isSymmetry(temp)){ cnt++; } } cout<<cnt<<endl; return 0; }
/* 思想:首先写一个回文字符串的判断方法,然后分别将B字符串插入到A的不同位置,把每次生成的字符串进行回文判断 */ import java.io.BufferedReader; import java.io.InputStreamReader; //import java.util.Arrays; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String A = br.readLine(); String B = br.readLine(); int count = 0; for(int i = 0;i<=A.length();i++){ StringBuffer sb = new StringBuffer(); sb.append(A.substring(0,i)); sb.append(B); sb.append(A.substring(i,A.length())); if(isHui(sb.toString())) count++; } System.out.println(count); } public static boolean isHui(String str){ //两边向内的方法 if(str.length()==0)return false; if(str.length()==1)return true; int left = 0,right = str.length()-1; while(left<right){ if(str.charAt(left) == str.charAt(right)){ left++; right--; }else{ return false; } } return true; } }
//详情请点击博客链接(链接在最下方) import java.util.Scanner; public class Statistical_Palindrome2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String s1 = scanner.nextLine(); String s2 = scanner.nextLine(); int count=0; //第一步:截取插入并连接字符串 //(这里用StringBuilder能解决String不可变性的短板,不用StringBuffer的原因是用不着线程安全效率快) for (int i = 0; i <= s1.length(); i++) { StringBuilder s = new StringBuilder(s1); s.insert(i, s2); //通过for循环在s1的每个位置都插入得到所有可能出现的字符串 if(isPalindromic(s)){ count++; } } System.out.println(count); } //第二步:判断一个字符串是否是回文字符串 public static boolean isPalindromic(StringBuilder s) { int i = 0; int j = s.length() - 1; while (i <= j) { //取出新得到的字符串挨个字符进行比较 if (s.charAt(i) != s.charAt(j)) { return false; } i++; j--; } return true; } } ———————————————— 版权声明:本文为CSDN博主「峰回路转」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_44840148/article/details/105123181
#include <bits/stdc++.h> using namespace std; inline char getForChar(string &s1, string &s2, int inser, int index){ int ss2 = s2.size(); if(inser>index) return s1[index]; else if(inser+ss2>index) return s2[index-inser]; else return s1[index-ss2]; } int huiwen(string &s1,string &s2){ int ss1 = s1.size(); int ss2 = s2.size(); int begin = 0; int end = ss1+ss2-1; int count = 0; for(int inser = 0; inser<=ss1; ++inser){ begin = 0; end = ss1+ss2-1; while(begin<=end){ if(getForChar(s1,s2,inser,begin)!=getForChar(s1,s2,inser,end)){ break; } else { begin++; end--; } } if(begin>=end) count++; } return count ; } int main(){ string str1,str2; getline(cin,str1); getline(cin,str2); cout << huiwen(str1,str2); return 0; }
#include<iostream> #include<string> using namespace std; bool TractString(string s) { int i = 0, j = s.length()-1; while (1) { if (s[i] != s[j]) return false; i++; j--; if (i >= j) break; } return true; } int main() { string s1, s2; int rec=0; cin >> s1>>s2; for (int i = 0; i <=s1.length(); i++) { string s3 = { "" }; for (int j = 0; j <s1.length(); j++) { if (j!=i) s3 += s1[j]; else { s3 += s2; s3 += s1[j]; } } if (i == s1.length()) s3 = s1 + s2; if (TractString(s3)) rec++; } cout << rec << endl; return 0; }
#include<algorithm> #include<iostream> #include<vector> #include<string> using namespace std; bool isValidPalindrome(string s) { int l = 0, r = s.size() - 1; bool flag = true; while (l <= r) { if (s[l] != s[r]){ flag = false; break; } else{ l++; r--; } } return flag; } int main() { string A, B; cin >> A >> B; int cnt = 0; for (int i = 0; i <= A.size(); i++) { string tmp=A; tmp.insert(i,B); if (isValidPalindrome(tmp)) cnt++; else continue; } cout << cnt << endl; }
//写好了这个才看到别人答案直接用一个insert就解决了 笑哭 #include<iostream> #include<string> #include<sstream> #include<vector> using namespace std; bool isb(string s){//判断回文 string s1=s; int len=s.length(); for(int i=0;i<len;i++){ s1[len-1-i]=s[i]; } return s==s1; } string ch2str(char ch){//将char转化为string string str; stringstream ss; ss << ch; str = ss.str(); return str; } int main(){ string A,B,temp; int cnt=0;//cnt表示结果 cin>>A>>B; int a=A.length(); vector<string>ss(a); for(int i=0;i<a;i++){//将A的字母单个拆分转化成string存在vector<string>ss(A.length())中 ss[i]=ch2str(A[i]); } for(int i=0;i<a;i++){ //cout<<" i: "<<i ; ss[i]+=B;//用 ch2str(A[i])+B 替换掉ss[i] //cout<<" ss"<<"["<<i<<"]: "<<ss[i]; for(int j=0;j<a;j++){//替换完再按顺序拼接,存进temp temp+=ss[j]; //cout<<" temp: "<<temp; } ss[i]= ch2str(A[i]);//复原ss[i] if(isb(temp)){ cnt++; //cout<<" cnt "<<cnt; } temp.clear(); } cout<<endl; if(isb(B+A)){cnt++;}//前面的情况差B完全插在A的最前面这一种 cout<<cnt<<endl; return 0; }
s = input() a = input() connect = '' num = 0 for i in range(0,len(s)+1): if i == 0: connect = a+s elif i == len(s): connect = s+a else: connect = s[0:i]+a+s[i:len(s)] if connect == connect[::-1]: num += 1 print(num) 这题很简单,时间主要用来思考如何让代码看上去更简洁更美观上了。 然而看到有用两行Python大法的大神,无奈自己还是太弱鸡。