如何分割字符串成单词。例如:" stringintowords" - > "串入词QUOT;?单词、字符、串成、QUOT

2023-09-10 23:00:33 作者:?爱他怎样毁我如何、

什么是正确的方式来分割字符串转换成的话? (字符串中不包含任何空格或标点符号)

What is the right way to split a string into words ? (string doesn't contain any spaces or punctuation marks)

例如:stringintowords - >字符串成字

For example: "stringintowords" -> "String Into Words"

能否请您指教一下算法应该用在这里?

Could you please advise what algorithm should be used here ?

!更新:对于那些谁认为这个问题只是出于好奇。这个算法可以用来camеlcase域名(sportandfishing .COM - >SportAndFishing .COM)。与此算法中,目前所使用的公司简介点组织做动态这种转换

! Update: For those who think this question is just for curiosity. This algorithm could be used to camеlcase domain names ("sportandfishing .com" -> "SportAndFishing .com") and this algo is currently used by aboutus dot org to do this conversion dynamically.

推荐答案

正如很多人在这里,这是一个标准的,简单的动态规划问题:最好的解决办法是由福尔克Hüffner给出。虽然附加信息:

As mentioned by many people here, this is a standard, easy dynamic programming problem: the best solution is given by Falk Hüffner. Additional info though:

(一),你应该考虑实施 isWord 有线索,如果你使用正确的(即通过逐步测试的话)。这将节省您大量的时间

(a) you should consider implementing isWord with a trie, which will save you a lot of time if you use properly (that is by incrementally testing for words).

(二)输入分段动态规划得到了更详细的答案的分数,从与伪code算法校级讲座,如的本次讲座在杜克大学的(甚至走得更远,提供一个简单的概率方法来处理,当你有话该怎么做被包含在任何字典)。

(b) typing "segmentation dynamic programming" yields a score of more detail answers, from university level lectures with pseudo-code algorithm, such as this lecture at Duke's (which even goes so far as to provide a simple probabilistic approach to deal with what to do when you have words that won't be contained in any dictionary).