-3

I'm new here and also new to Python. So, this is my question and I really don't know where to start. I created a list and wrote liste.add(str(input('input a string'))). Can you help me, thanks.

I have N strings and the target is to obtain maximum points by concatenating some of these strings. If we want to add string "B" after string "A" they should satisfy rules below:

"A" should be lexicographically smaller than "B".

● Some suffix of "A" (with minimum length of 1) should be same with some prefix of "B". Ex: last three characters of string “abaca” is same with first three characters of string “acaba”.

● After concatenating string "A" and "B", we gain points equal to length of their overlap(3 for example above)

Range 1 ≤ N ≤ 500

1 ≤ | Si | ≤ 500 (Length of any string)

Input Format:

In the first line there will be number (number of strings). Next lines there will be strings we have (all of them contains only lowercase English character)

Output Format:

In single line, print the maximum points can user get.

Sample Input : 4 a ba ab acaba

Sample Output : 3

Explanation:

With a - acaba - ba order, user can get 1 + 2 = 3 points.

ZF007
  • 3,708
  • 8
  • 29
  • 48
helloworld
  • 11
  • 2
  • snake bite...post code like [this](https://stackoverflow.com/help/mcve).. or get question removed... bye world... ;-) – ZF007 Jan 20 '18 at 23:36
  • this is not a code. according to this link, i think you tried to say your post should be minimal. but everything in this question is a must and i should say everything to let people understand the question. sorry. – helloworld Jan 21 '18 at 12:25
  • the link I provided you explains how to post a question. You post a wishlist, cross fingers and hope we do your homework for you. Apparently.. QFlat was silly enough to do so. But hey... QFlat show how YOU should post your code (in a question (w/o throwing an error)). Most-likely something with an error that you tried to solve, which obviously is explained as well according to mcve rules. The downvoting of your question is because you didn't comply to mcve or in general showed little or no effort to solve it yourself. To be frank... buy a python (cook)book and try to understand their code. – ZF007 Jan 21 '18 at 13:23
  • i tried. " N = int(input("number of string: ")) liste1 = [] for i in range(N): i = str(input("input a string")) liste1.append(i) while liste1[0::0] == liste1[1::0]: continue" this was my very first code but if you can check the answer down below you can see how unrelated it is. i mentioned earlier that i am new to python. this is not an homework. this is semester lol and we do not have any python class, i am a business management student :) i needed a good start and thanks for the answer down below i started with this and continued with my own way. just chill lol – helloworld Jan 21 '18 at 14:08

1 Answers1

0

Firstly, I went ahead and got some sample code to run in my machine to fit your needs for an input. I also took the liberty of adding some additions to make sure your input's requirements were fully met. Here are my results below:

import itertools
n = int(input('Number of strings: '))
while n not in range(1,501):
    print('Error, new number needed')
    n = int(input('Number of strings: '))
strings = []
a = 0
for i in range(0,n):
    string = str(input('String: '))
    while len(strings[a]) < len(string):
        print('Error: new string must be longer than previous input')
        string = str(input('String: '))
    strings.append(string)

If you notice, I imported itertools. That is because if you would like every combination of strings in your list, itertools will help you get that done. I have also provided the code found on Get every combination of strings (if you click on that, it'll take you to the page), where they go over this topic. I'm not a professional on itertools, but hopefully the attached code should be a good leap forward:

S = set(['a', 'ab', 'ba'])

collect = set()
step = set([''])
while step:
    step = set(a+b for a in step for b in S if len(a+b) <= 6)
collect |= step

print sorted(collect)

If you have any other questions, please let me know. Happy programming!

  • 1
    thank you so much! you really helped a lot, i am very thankful :) first, i could not find any defined b as a variable. step = set(a+b for a in step for b in S if len(a+b) <= 6) but there is no b. and also i dont understand where this 6 comes from. thanks a lot again!! – helloworld Jan 21 '18 at 12:23
  • The code I attached from the other link was just an example. You would have to go in and change portions of the code yourself. The above code exemplifies how to get every combination of strings from a list, so making a few changes to the code to fit your needs shouldn't be too difficult. – Jaden Costa Jan 22 '18 at 14:40