Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7689999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:17:46+00:00 2026-05-31T20:17:46+00:00

I read in an array of strings such as: aaa bb ccccc ddd eeee

  • 0

I read in an array of strings such as: aaa bb ccccc ddd eeee fffffff ggggggg. I need help working on an algorithm to fit these strings on as little lines as possible, with the maxium ammount of charcters on a line being a fixed value, for example 15. If adding another string to that line exceeds this value, I need to make a new line.

I thought searching through, finding the largest string, then concatenating with the smallest, then the concatenating that with next largest … and so on would work, but it doesnt achieve the results I expected, any other ideas?

The output I need would look like:

aaa bb ddd eeee
fffffff ggggggg

As each line has 15 charcters on it, and this is the smallest ammount of lines you could possibly have.

I’m using C sharp.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:17:48+00:00Added an answer on May 31, 2026 at 8:17 pm

    I think what you are doing is a semi-greedy algorithm, which in this case would not give you an optimal solution. The optimal solution can be found using something like dynamic programming with memoization. I would refer you to the examples in the wikipedia article on dynamic programming. But to give you a general idea, you want to do the following:

    Start filling a line with chunck of characters ( of any size)
    once you cannot fit a new word, check if adding the left-out word, ‘instead’ of one of the words in the line would give you a better fill quote.
    if so replace, if not leave that word out, try a new one. ( by new one I mean different size, I assume you hash all words of similar size into same buckets)
    once you have tried all sizes you can move on to the next line, and do the same, however, you need to also check if swapping elements between first and second lines would give you a better “total” result. Implementing this can be done in n^2, but is a bit trickier, if you don’t care much about the computational efficiency, just do this brute force.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to read a text file into an array of strings using
I'm working on an application in C# and need to read and write from
I have read this document Initializing static array of strings (C++)? and tried to
//assign variables string measurementIn; //read in file in array string[] lines = File.ReadAllLines(../../convert.txt); //ask
How to read an array of numbers from a file? I mean, how to
I read in a Byte Array generated from a function called from an external
Given following Ruby statements: (Read input and store each word in array removing spaces
Is it possible to read the data in the php $_SESSION array in the
In java i would read the whole file in to byte array and do
I read that Python does not actually support 2D arrays but rather an array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.