#coding:utf-8
def no_abba(str):
#破坏回文串使之不能包含回文
strnew =""
for i in range(len(str)):
if len(strnew)==0:
strnew=str[i]
if (len(strnew)==1)and (strnew[-1]!=str[i]):
strnew+=str[i]
if (len(strnew)>1)and(strnew[-1]!=str[i])and (strnew[-2]!=str[i]):
strnew +=str[i]
print strnew
def del_abba(str):
#删除回文串使之不能包含回文
strnew =""
for i in range(len(str)):
if len(strnew)==0:
strnew=str[i]
elif(strnew[-1]==str[i]):
strnew=strnew[:-1]
elif(len(strnew)>1):
if(strnew[-1]!=str[i])and (strnew[-2]==str[i]):
strnew = strnew[:-2]
else:
strnew +=str[i]
else:
strnew +=str[i]
print strnew
if __name__=="__main__":
a="1234abcbacdfaf"
no_abba(a)
del_abba(a)
Python处理字符串中的回文串(“回文串”是一个正读和反读都一样的字符串,比如“level”、“aba”或者“noon”、“aa”等等就是回文串)
最新推荐文章于 2023-12-10 17:07:16 发布