- 資料結構與演算法
- DSA - 首頁
- DSA - 概述
- DSA - 環境設定
- DSA - 演算法基礎
- DSA - 漸進分析
- 資料結構
- DSA - 資料結構基礎
- DSA - 資料結構和型別
- DSA - 陣列資料結構
- 連結串列
- DSA - 連結串列資料結構
- DSA - 雙向連結串列資料結構
- DSA - 迴圈連結串列資料結構
- 棧與佇列
- DSA - 棧資料結構
- DSA - 表示式解析
- DSA - 佇列資料結構
- 搜尋演算法
- DSA - 搜尋演算法
- DSA - 線性搜尋演算法
- DSA - 二分搜尋演算法
- DSA - 插值搜尋
- DSA - 跳躍搜尋演算法
- DSA - 指數搜尋
- DSA - 斐波那契搜尋
- DSA - 子列表搜尋
- DSA - 雜湊表
- 排序演算法
- DSA - 排序演算法
- DSA - 氣泡排序演算法
- DSA - 插入排序演算法
- DSA - 選擇排序演算法
- DSA - 歸併排序演算法
- DSA - 希爾排序演算法
- DSA - 堆排序
- DSA - 桶排序演算法
- DSA - 計數排序演算法
- DSA - 基數排序演算法
- DSA - 快速排序演算法
- 圖資料結構
- DSA - 圖資料結構
- DSA - 深度優先遍歷
- DSA - 廣度優先遍歷
- DSA - 生成樹
- 樹資料結構
- DSA - 樹資料結構
- DSA - 樹的遍歷
- DSA - 二叉搜尋樹
- DSA - AVL樹
- DSA - 紅黑樹
- DSA - B樹
- DSA - B+樹
- DSA - 伸展樹
- DSA - 字典樹
- DSA - 堆資料結構
- 遞迴
- DSA - 遞迴演算法
- DSA - 使用遞迴實現漢諾塔
- DSA - 使用遞迴實現斐波那契數列
- 分治法
- DSA - 分治法
- DSA - 最大最小問題
- DSA - Strassen矩陣乘法
- DSA - Karatsuba演算法
- 貪心演算法
- DSA - 貪心演算法
- DSA - 旅行商問題(貪心演算法)
- DSA - Prim最小生成樹
- DSA - Kruskal最小生成樹
- DSA - Dijkstra最短路徑演算法
- DSA - 地圖著色演算法
- DSA - 分數揹包問題
- DSA - 帶截止日期的作業排序
- DSA - 最優合併模式演算法
- 動態規劃
- DSA - 動態規劃
- DSA - 矩陣鏈乘法
- DSA - Floyd Warshall演算法
- DSA - 0-1揹包問題
- DSA - 最長公共子序列演算法
- DSA - 旅行商問題(動態規劃)
- 近似演算法
- DSA - 近似演算法
- DSA - 頂點覆蓋演算法
- DSA - 集合覆蓋問題
- DSA - 旅行商問題(近似演算法)
- 隨機化演算法
- DSA - 隨機化演算法
- DSA - 隨機化快速排序演算法
- DSA - Karger最小割演算法
- DSA - Fisher-Yates洗牌演算法
- DSA有用資源
- DSA - 問答
- DSA - 快速指南
- DSA - 有用資源
- DSA - 討論
單詞拆分問題
什麼是單詞拆分問題?
單詞拆分問題是計算機科學中的一道邏輯謎題,要求我們檢查給定的字串是否可以分割成字典中的一系列單詞。例如,如果給定的字串是“ramhaspenandapple”,字典是["ram", "and", "has", "apple", "pen"],則答案為真,因為該字串可以分割成“ram has pen and apple”。
使用回溯法解決單詞拆分問題
在本問題的輸入中,給出一個沒有空格的句子,另一個字典也提供了一些有效的英文單詞。我們必須找到將句子分解成單個字典單詞的所有可能方法。給定的字串和字典如下:
Dictionary: {ram, samuel, winter, man, mango, icecream, and, i, love, ice, cream}
Given String: "ilovewinterandicecream"
我們將嘗試從字串的左側開始搜尋以找到一個有效的單詞,當找到一個有效的單詞時,我們將搜尋該字串下一部分中的單詞。將字串分解成給定單詞的所有可能方法如下:
i love winter and ice cream i love winter and icecream
解決此問題的一種方法是使用回溯法。這是一種嘗試不同組合並在部分解決方案無效時回溯的技術。基本思想是從字串的開頭開始,檢查字首是否為指定字典中的單詞。如果是,則我們遞迴地檢查剩餘的字尾是否可以分割成單詞。如果字首和字尾都有效,則我們返回true並將其標記為解決方案的一部分。否則,我們回溯並嘗試不同的字首。
虛擬碼
以下是使用回溯法解決單詞拆分問題的虛擬碼:
Begin
for i := 0 to n, do
subStr := substring of given string from (0..i)
if subStr is in dictionary, then
if i = n, then
result := result + subStr
display the result
return
wordBreak(substring from (i..n-i), n-i, result, subStr, ‘space’)
done
End
示例
在下面的示例中,我們將實際演示如何解決單詞拆分問題。
#include <stdio.h>
#include <string.h>
#define N 13
char *dictionary[N] = {"mobile","samsung","sam","sung","man","mango", "icecream","and", "go","i","love","ice","cream"};
//check whether the word is in dictionary or not
int isInDict(char *word){
for (int i = 0; i < N; i++)
if (strcmp(dictionary[i], word) == 0)
return 1;
return 0;
}
void wordBreak(char *str, int n, char *result) {
for (int i=1; i<=n; i++) {
//get string from 0 to ith location of the string
char subStr[100];
strncpy(subStr, str, i);
subStr[i] = '\0';
//if subStr is found in the dictionary
if (isInDict(subStr)) {
if (i == n) {
//add substring in the result
strcat(result, subStr);
printf("%s\n", result);
return;
}
//otherwise break rest part
char newResult[100];
strcpy(newResult, result);
strcat(newResult, subStr);
strcat(newResult, " ");
wordBreak(str + i, n-i, newResult);
}
}
}
int main() {
char str[] = "iloveicecreamandmango";
char result[100] = "";
wordBreak(str, strlen(str), result);
return 0;
}
#include <iostream>
#define N 13
using namespace std;
string dictionary[N] = {"mobile","samsung","sam","sung","man","mango", "icecream","and", "go","i","love","ice","cream"};
//check whether the word is in dictionary or not
int isInDict(string word){
for (int i = 0; i < N; i++)
if (dictionary[i].compare(word) == 0)
return true;
return false;
}
void wordBreak(string str, int n, string result) {
for (int i=1; i<=n; i++) {
//get string from 0 to ith location of the string
string subStr = str.substr(0, i);
//if subStr is found in the dictionary
if (isInDict(subStr)) {
if (i == n) {
//add substring in the result
result += subStr;
cout << result << endl;
return;
}
//otherwise break rest part
wordBreak(str.substr(i, n-i), n-i, result + subStr + " ");
}
}
}
int main() {
string str="iloveicecreamandmango";
wordBreak(str, str.size(),"");
}
import java.util.Arrays;
import java.util.List;
public class Main {
static final List<String> DICTIONARY = Arrays.asList("mobile","samsung","sam","sung","man","mango", "icecream","and", "go","i","love","ice","cream");
//check whether the word is in dictionary or not
public static boolean isInDict(String word){
return DICTIONARY.contains(word);
}
public static void wordBreak(String str, int n, String result) {
for (int i=1; i<=n; i++) {
//get string from 0 to ith location of the string
String subStr = str.substring(0, i);
//if subStr is found in the dictionary
if (isInDict(subStr)) {
if (i == n) {
//add substring in the result
result += subStr;
System.out.println(result);
return;
}
//otherwise break rest part
wordBreak(str.substring(i, n), n-i, result + subStr + " ");
}
}
}
public static void main(String[] args) {
String str = "iloveicecreamandmango";
wordBreak(str, str.length(), "");
}
}
# Define the dictionary
dictionary = ["mobile","samsung","sam","sung","man","mango", "icecream","and", "go","i","love","ice","cream"]
# Check whether the word is in dictionary or not
def isInDict(word):
return word in dictionary
# Function to break the word
def wordBreak(str, n, result):
for i in range(1, n+1):
# Get string from 0 to ith location of the string
subStr = str[:i]
# If subStr is found in the dictionary
if isInDict(subStr):
if i == n:
# Add substring in the result
result += subStr
print(result)
return
# Otherwise break rest part
wordBreak(str[i:], n-i, result + subStr + " ")
# Main function
def main():
str = "iloveicecreamandmango"
wordBreak(str, len(str), "")
# Call the main function
if __name__ == "__main__":
main()
輸出
i love ice cream and man go i love ice cream and mango i love icecream and man go i love icecream and mango
廣告