From 2272d0e1e303f93d6a61e33674c799d848817824 Mon Sep 17 00:00:00 2001 From: Yang-Changhui <2275029710@qq.com> Date: Mon, 5 Aug 2024 23:05:41 +0800 Subject: [PATCH] =?UTF-8?q?add=20problems/kamacoder/0110.=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E6=8E=A5=E9=BE=99.md=20python?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/kamacoder/0110.字符串接龙.md | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/problems/kamacoder/0110.字符串接龙.md b/problems/kamacoder/0110.字符串接龙.md index feeec6dd..5d13e368 100644 --- a/problems/kamacoder/0110.字符串接龙.md +++ b/problems/kamacoder/0110.字符串接龙.md @@ -217,6 +217,38 @@ public class Main { ``` ### Python +```Python +def judge(s1,s2): + count=0 + for i in range(len(s1)): + if s1[i]!=s2[i]: + count+=1 + return count==1 + +if __name__=='__main__': + n=int(input()) + beginstr,endstr=map(str,input().split()) + if beginstr==endstr: + print(0) + exit() + strlist=[] + for i in range(n): + strlist.append(input()) + + # use bfs + visit=[False for i in range(n)] + queue=[[beginstr,1]] + while queue: + str,step=queue.pop(0) + if judge(str,endstr): + print(step+1) + exit() + for i in range(n): + if visit[i]==False and judge(strlist[i],str): + visit[i]=True + queue.append([strlist[i],step+1]) + print(0) +``` ### Go