Title Description
To define a word “ Brother word ” by : Exchange the alphabetical order of the word , Without adding 、 Delete 、 Words that can be generated by modifying the original letters .
Brother word requirements are different from the original word . for example :ab and ba It's a brother word .ab and ab It's not a brother word .
Now give you n Word , I'll give you another word str, Let you find str In my brother's word , Dictionary preface No k What is the big word ?
Be careful : There may be duplicate words in the dictionary . This question contains multiple groups of input data .
Input description :
First enter the number of words n, Input again n Word .
Enter another word , For the word to be found x
Finally, enter the number k
Output description :
Output found x The number of brothers' words m
Then output the... Found after sorting according to the dictionary order k A brother word , Not in accordance with article k You don't have to output .
Example 1
Input
3 abc bca cab abc 1
Output
2
bca
The code implementation is as follows :
import itertools
def func():
while True:
try:
lists = input().split()
n = lists[0]
str_list = lists[1:-2]
search = lists[-2]
num = int(lists[-1])
res = []
for i in itertools.permutations(search):
v = ''.join(list(i))
#print(i)
if v != search and (v not in res):
for j in str_list:
#print("==v:",v,str_list.count(v))
if v == j :
res.append(v)
res.sort()
#print(res)
print(len(res))
if len(res)>=num-1:
print(res[num-1])
except Exception as e:
#print(e)
break
if __name__ == '__main__':
func()