@@ -183,3 +183,5 @@ Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/eb64263dd3de4a08/22_ROTATE.ipch | |||||
Alogrithm/.vs/Alogrithm/v16/TestStore/0/007.testlog | Alogrithm/.vs/Alogrithm/v16/TestStore/0/007.testlog | ||||
Alogrithm/Alogrithm/Debug/22_Rotate.obj | Alogrithm/Alogrithm/Debug/22_Rotate.obj | ||||
Alogrithm/UnitTest/Debug/UnitTest.tlog/link.15328.delete.1.tlog | Alogrithm/UnitTest/Debug/UnitTest.tlog/link.15328.delete.1.tlog | ||||
*.tlog | |||||
*.obj |
@@ -0,0 +1,16 @@ | |||||
[Test1] | |||||
Input1=cppcode | |||||
Input2=cpp,code | |||||
Output=1 | |||||
[Test2] | |||||
Input1=catsanddog | |||||
Input2=cats,sand,dog | |||||
Output=0 | |||||
[Test3] | |||||
Input1=catsanddog | |||||
Input2=cats,and,dog | |||||
Output=1 | |||||
[Test4] | |||||
Input1=catsanddog | |||||
Input2=cats,and,dog | |||||
Output=1 |
@@ -0,0 +1,6 @@ | |||||
#pragma once | |||||
#include <string.h> | |||||
#include <stdlib.h> | |||||
#include <stdio.h> | |||||
bool WordBreak(char* s, char** wordDict, int wordDictSize); |
@@ -0,0 +1,44 @@ | |||||
#include "../include/17_WordBreak.h" | |||||
//题目:给定一个非空字符串s和一个包含非空单词列表的字典wordDict, | |||||
// 判定s是否可以被空格拆分为一个或多个在字典中出现的单词 | |||||
//思路:从字符串s的第一个字符开始,拿s的前word_len个字符和字典中的元素比较,如果匹配到相同的,则对应的枚举位置1 | |||||
// 每次枚举位置1前先考虑上一位是否为也为1,如果不为1就置0,一直到最后一个字符,返回最后一个字符的枚举dp[s_len] | |||||
bool WordBreak(char* s, char** wordDict, int wordDictSize) { | |||||
int s_len = strlen(s); // 获取s的长度 | |||||
//异常情况判断 | |||||
if (wordDict == NULL || wordDictSize == 0 || s == NULL) { | |||||
return false; | |||||
} | |||||
// 定义一个dp数组,表示当前下标时前面的字母是否可以满足被拆分条件,并都初始化为false | |||||
bool *dp = (bool *)malloc(sizeof(bool)*(s_len + 1)); | |||||
memset(dp, false, sizeof(bool) * (s_len + 1)); | |||||
dp[0] = true; // 0需要被初始化为true | |||||
// 从第一个字母开始往后遍历 | |||||
for (int i = 1; i <= s_len; i++) { | |||||
// 从第一个单词开始遍历 | |||||
for (int j = 0; j < wordDictSize; j++) { | |||||
// 获取当前比较的单词长度 | |||||
int word_len = strlen(wordDict[j]); | |||||
// s当前位置往前退word_len个位置开始和worDict进行比较是否相等 | |||||
int k = i - word_len; //k同时也用来访问dp[]的上一次的状态 | |||||
// k < 0表示退过头了,肯定不满足条件 | |||||
if (k < 0) { | |||||
continue; | |||||
} | |||||
// 如果当前i位置和当前比较的wordDict正好相等,那么前提是k位置之前也要都满足条件,否则前面的不满足,即使现在从k~i满足也没有用 | |||||
if (strncmp(s + k, wordDict[j], word_len) == 0) { | |||||
dp[i] = (dp[k] && dp[0]); | |||||
} | |||||
// 如果i位置满足条件了,则不需要再比较后面的worDict了,直接比较下一个i的位置即可 | |||||
if (dp[i] == 1) { | |||||
break; | |||||
} | |||||
} | |||||
} | |||||
for (int t = 0; t < s_len; t++) | |||||
printf("%d ", dp[t]); | |||||
// 返回s最后一个位置是否满足切分条件即可 | |||||
return dp[s_len]; | |||||
} |
@@ -18,12 +18,16 @@ using namespace Microsoft::VisualStudio::CppUnitTestFramework; | |||||
#define FileName_14 "../Alogrithm/config/14_SingleNumber.ini" | #define FileName_14 "../Alogrithm/config/14_SingleNumber.ini" | ||||
#define FileName_15 "../Alogrithm/config/15_WordPattern.ini" | #define FileName_15 "../Alogrithm/config/15_WordPattern.ini" | ||||
#define FileName_16 "../Alogrithm/config/16_ReverseBits.ini" | #define FileName_16 "../Alogrithm/config/16_ReverseBits.ini" | ||||
#define FileName_17 "../Alogrithm/config/17_WordBreak.ini" | |||||
#define FileName_18 "../Alogrithm/config/18_PlusOne.ini" | #define FileName_18 "../Alogrithm/config/18_PlusOne.ini" | ||||
#define FileName_19 "../Alogrithm/config/19_MySqrt.ini" | #define FileName_19 "../Alogrithm/config/19_MySqrt.ini" | ||||
#define FileName_20 "../Alogrithm/config/20_MoveZeroes.ini" | #define FileName_20 "../Alogrithm/config/20_MoveZeroes.ini" | ||||
#define FileName_21 "../Alogrithm/config/21_Reverse.ini" | #define FileName_21 "../Alogrithm/config/21_Reverse.ini" | ||||
#define FileName_22 "../Alogrithm/config/22_Rotate.ini" | #define FileName_22 "../Alogrithm/config/22_Rotate.ini" | ||||
namespace UnitTest | namespace UnitTest | ||||
{ | { | ||||
TEST_CLASS(UnitTest_1) | TEST_CLASS(UnitTest_1) | ||||
@@ -314,6 +318,26 @@ namespace UnitTest | |||||
} | } | ||||
} | } | ||||
}; | }; | ||||
TEST_CLASS(UnitTest_17) | |||||
{ | |||||
TEST_METHOD(TestMethode1) | |||||
{ | |||||
char Section_Name[100][10] = { 0 }; | |||||
int Section_Count = CalcCount(100, Section_Name, FileName_17); | |||||
CString Input1, Input2,Output; | |||||
char Str_char[1024] = { 0 }; | |||||
char *WordDic[100]; | |||||
for (int i = 0; i < Section_Count; i++) { | |||||
GetPrivateProfileString(Section_Name[i], "Input1", " ", Input1.GetBuffer(200), 200, FileName_17); | |||||
GetPrivateProfileString(Section_Name[i], "Input2", " ", Input2.GetBuffer(500), 500, FileName_17); | |||||
GetPrivateProfileString(Section_Name[i], "Output", " ", Output.GetBuffer(20), 20, FileName_17); | |||||
strcpy(Str_char, Input1); | |||||
int Wordic_count = str_device3(Input2, WordDic); | |||||
bool nReal = WordBreak(Str_char, WordDic, Wordic_count); | |||||
Assert::AreEqual(nReal, CstrToBool(Output)); | |||||
} | |||||
} | |||||
} | |||||
TEST_CLASS(UnitTest_18) | TEST_CLASS(UnitTest_18) | ||||
{ | { | ||||
TEST_METHOD(TestMethode1) | TEST_METHOD(TestMethode1) | ||||
@@ -88,6 +88,24 @@ int str_device2(CString str, char(*return_str)[50]) | |||||
} | } | ||||
return value_count; //返回数组首地址 | return value_count; //返回数组首地址 | ||||
} | } | ||||
int str_device3(CString str, char* *return_str) | |||||
{ | |||||
int value_count = 0; | |||||
char* token; //存放被切割后的第一个子串 | |||||
char Section_value[500] = { 0 };//存放nums转换成string类型的结果 | |||||
memset(Section_value, 0, sizeof(char) * 500); | |||||
strcpy(Section_value, str);//将CString类型的字符串转换成char类型,方便后面切割字符串 | |||||
//获得切割到的第一个字符串 | |||||
token = strtok(Section_value, ","); | |||||
/* 继续获取其他的子字符串 */ | |||||
while (token != NULL) { | |||||
return_str[value_count] = (char*)malloc(sizeof(char) * 100); | |||||
strcpy(return_str[value_count], token); | |||||
token = strtok(NULL, ","); | |||||
value_count++; //记录存了多少个元素 | |||||
} | |||||
return value_count; //返回数组首地址 | |||||
} | |||||
//字符串转bool | //字符串转bool | ||||
bool CstrToBool(CString str) | bool CstrToBool(CString str) | ||||
{ | { | ||||
@@ -29,6 +29,7 @@ | |||||
#include"../Alogrithm/include/14_SingleNumber.h" | #include"../Alogrithm/include/14_SingleNumber.h" | ||||
#include"../Alogrithm/include/15_WordPattern.h" | #include"../Alogrithm/include/15_WordPattern.h" | ||||
#include"../Alogrithm/include/16_ReverseBits.h" | #include"../Alogrithm/include/16_ReverseBits.h" | ||||
#include"../Alogrithm/include/17_WordBreak.h" | |||||
#include"../Alogrithm/include/18_PlusOne.h" | #include"../Alogrithm/include/18_PlusOne.h" | ||||
#include"../Alogrithm/include/19_MySqrt.h" | #include"../Alogrithm/include/19_MySqrt.h" | ||||
#include"../Alogrithm/include/20_MoveZeroes.h" | #include"../Alogrithm/include/20_MoveZeroes.h" | ||||
@@ -36,9 +37,11 @@ | |||||
#include"../Alogrithm/include/22_Rotate.h" | #include"../Alogrithm/include/22_Rotate.h" | ||||
int CalcCount(int n, char(*str)[10],const char *FileName); | int CalcCount(int n, char(*str)[10],const char *FileName); | ||||
int* str_device(CString str, int* value_count); | int* str_device(CString str, int* value_count); | ||||
int str_device2(CString str, char(*return_str)[50]); | int str_device2(CString str, char(*return_str)[50]); | ||||
int str_device3(CString str, char**return_str); | |||||
bool CstrToBool(CString str); | bool CstrToBool(CString str); | ||||