@@ -154,3 +154,9 @@ Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/5ccdb9c64312b2ee/16_REVERSEBITS.ipch | |||||
Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/969dd2cbf6a00d1b/16_REVERSEBITS.ipch | Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/969dd2cbf6a00d1b/16_REVERSEBITS.ipch | ||||
Alogrithm/.vs/Alogrithm/v16/TestStore/0/003.testlog | Alogrithm/.vs/Alogrithm/v16/TestStore/0/003.testlog | ||||
Alogrithm/Alogrithm/Debug/16_ReverseBits.obj | Alogrithm/Alogrithm/Debug/16_ReverseBits.obj | ||||
Alogrithm/.vs/Alogrithm/v16/ipch/AutoPCH/13d1a063a12e2911/17_WORDBREAK.ipch | |||||
Alogrithm/Alogrithm/Debug/17_WordBreak.obj | |||||
*.tlog | |||||
*.coverage | |||||
*.testlog | |||||
*.ipch |
@@ -89,6 +89,7 @@ | |||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> | ||||
<ConformanceMode>false</ConformanceMode> | <ConformanceMode>false</ConformanceMode> | ||||
<ModuleOutputFile>$(IntDir)%(RelativeDir)</ModuleOutputFile> | <ModuleOutputFile>$(IntDir)%(RelativeDir)</ModuleOutputFile> | ||||
<LanguageStandard_C>stdc11</LanguageStandard_C> | |||||
</ClCompile> | </ClCompile> | ||||
<Link> | <Link> | ||||
<SubSystem>Console</SubSystem> | <SubSystem>Console</SubSystem> | ||||
@@ -148,6 +149,7 @@ | |||||
<ClCompile Include="src\14_SingleNumber.cpp" /> | <ClCompile Include="src\14_SingleNumber.cpp" /> | ||||
<ClCompile Include="src\15_WordPattern.cpp" /> | <ClCompile Include="src\15_WordPattern.cpp" /> | ||||
<ClCompile Include="src\16_ReverseBits.cpp" /> | <ClCompile Include="src\16_ReverseBits.cpp" /> | ||||
<ClCompile Include="src\17_WordBreak.cpp" /> | |||||
<ClCompile Include="src\1_ContainsNearbyDuplicate.cpp" /> | <ClCompile Include="src\1_ContainsNearbyDuplicate.cpp" /> | ||||
<ClCompile Include="src\2_ExcelSheetColumnTitle.cpp" /> | <ClCompile Include="src\2_ExcelSheetColumnTitle.cpp" /> | ||||
<ClCompile Include="src\3_bool IsUgly.cpp" /> | <ClCompile Include="src\3_bool IsUgly.cpp" /> | ||||
@@ -167,6 +169,7 @@ | |||||
<ClInclude Include="include\14_SingleNumber.h" /> | <ClInclude Include="include\14_SingleNumber.h" /> | ||||
<ClInclude Include="include\15_WordPattern.h" /> | <ClInclude Include="include\15_WordPattern.h" /> | ||||
<ClInclude Include="include\16_ReverseBits.h" /> | <ClInclude Include="include\16_ReverseBits.h" /> | ||||
<ClInclude Include="include\17_WordBreak.h" /> | |||||
<ClInclude Include="include\1_ContainsNearbyDuplicate.h" /> | <ClInclude Include="include\1_ContainsNearbyDuplicate.h" /> | ||||
<ClInclude Include="include\2_ExcelSheetColumnTitle.h" /> | <ClInclude Include="include\2_ExcelSheetColumnTitle.h" /> | ||||
<ClInclude Include="include\3_bool IsUgly.h" /> | <ClInclude Include="include\3_bool IsUgly.h" /> | ||||
@@ -185,6 +188,7 @@ | |||||
<None Include="config\14_SingleNumber.ini" /> | <None Include="config\14_SingleNumber.ini" /> | ||||
<None Include="config\15_WordPattern.ini" /> | <None Include="config\15_WordPattern.ini" /> | ||||
<None Include="config\16_ReverseBits.ini" /> | <None Include="config\16_ReverseBits.ini" /> | ||||
<None Include="config\17_WordBreak.ini" /> | |||||
<None Include="config\1_ContainsNearbyDuplicate.ini" /> | <None Include="config\1_ContainsNearbyDuplicate.ini" /> | ||||
<None Include="config\2_ExcelSheetColumnTiTle.ini" /> | <None Include="config\2_ExcelSheetColumnTiTle.ini" /> | ||||
<None Include="config\3_bool IsUgly.ini" /> | <None Include="config\3_bool IsUgly.ini" /> | ||||
@@ -75,6 +75,9 @@ | |||||
<ClCompile Include="src\16_ReverseBits.cpp"> | <ClCompile Include="src\16_ReverseBits.cpp"> | ||||
<Filter>源文件\src</Filter> | <Filter>源文件\src</Filter> | ||||
</ClCompile> | </ClCompile> | ||||
<ClCompile Include="src\17_WordBreak.cpp"> | |||||
<Filter>源文件\src</Filter> | |||||
</ClCompile> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<ClInclude Include="include\2_ExcelSheetColumnTitle.h"> | <ClInclude Include="include\2_ExcelSheetColumnTitle.h"> | ||||
@@ -125,6 +128,9 @@ | |||||
<ClInclude Include="include\16_ReverseBits.h"> | <ClInclude Include="include\16_ReverseBits.h"> | ||||
<Filter>头文件\include</Filter> | <Filter>头文件\include</Filter> | ||||
</ClInclude> | </ClInclude> | ||||
<ClInclude Include="include\17_WordBreak.h"> | |||||
<Filter>头文件\include</Filter> | |||||
</ClInclude> | |||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<None Include="config\3_bool IsUgly.ini"> | <None Include="config\3_bool IsUgly.ini"> | ||||
@@ -175,5 +181,8 @@ | |||||
<None Include="config\16_ReverseBits.ini"> | <None Include="config\16_ReverseBits.ini"> | ||||
<Filter>资源文件\config</Filter> | <Filter>资源文件\config</Filter> | ||||
</None> | </None> | ||||
<None Include="config\17_WordBreak.ini"> | |||||
<Filter>资源文件\config</Filter> | |||||
</None> | |||||
</ItemGroup> | </ItemGroup> | ||||
</Project> | </Project> |
@@ -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]; | |||||
} |
@@ -1,9 +1,7 @@ | |||||
#include<stdio.h> | #include<stdio.h> | ||||
#include "../include/16_ReverseBits.h" | |||||
int main() | int main() | ||||
{ | { | ||||
printf("%ld\n", ReverseBits(1)); | |||||
return 0; | return 0; | ||||
} | } |
@@ -18,6 +18,7 @@ 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" | |||||
namespace UnitTest | namespace UnitTest | ||||
@@ -310,4 +311,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)); | |||||
} | |||||
} | |||||
}; | |||||
} | } |
@@ -103,7 +103,7 @@ | |||||
<SubSystem>Windows</SubSystem> | <SubSystem>Windows</SubSystem> | ||||
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | <AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> | ||||
<GenerateDebugInformation>DebugFull</GenerateDebugInformation> | <GenerateDebugInformation>DebugFull</GenerateDebugInformation> | ||||
<AdditionalDependencies>../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;../Alogrithm/Debug/5_MinDepth.obj;../Alogrithm/Debug/6_ContainsDuplicate.obj;../Alogrithm/Debug/7_MaxDepth.obj;../Alogrithm/Debug/8_HammingWeight.obj;../Alogrithm/Debug/9_AddBinary.obj;../Alogrithm/Debug/10_BinaryTreePaths.obj;../Alogrithm/Debug/11_CanWinNim.obj;../Alogrithm/Debug/12_IsValid.obj;../Alogrithm/Debug/13_MyAtoi.obj;../Alogrithm/Debug/14_SingleNumber.obj;../Alogrithm/Debug/15_WordPattern.obj;../Alogrithm/Debug/16_ReverseBits.obj;%(AdditionalDependencies)</AdditionalDependencies> | |||||
<AdditionalDependencies>../Alogrithm/Debug/1_ContainsNearbyDuplicate.obj;../Alogrithm/Debug/2_ExcelSheetColumnTitle.obj;../Alogrithm/Debug/3_bool IsUgly.obj;../Alogrithm/Debug/4_IsPalindrome.obj;../Alogrithm/Debug/5_MinDepth.obj;../Alogrithm/Debug/6_ContainsDuplicate.obj;../Alogrithm/Debug/7_MaxDepth.obj;../Alogrithm/Debug/8_HammingWeight.obj;../Alogrithm/Debug/9_AddBinary.obj;../Alogrithm/Debug/10_BinaryTreePaths.obj;../Alogrithm/Debug/11_CanWinNim.obj;../Alogrithm/Debug/12_IsValid.obj;../Alogrithm/Debug/13_MyAtoi.obj;../Alogrithm/Debug/14_SingleNumber.obj;../Alogrithm/Debug/15_WordPattern.obj;../Alogrithm/Debug/16_ReverseBits.obj;../Alogrithm/Debug/17_WordBreak.obj;%(AdditionalDependencies)</AdditionalDependencies> | |||||
</Link> | </Link> | ||||
</ItemDefinitionGroup> | </ItemDefinitionGroup> | ||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> | ||||
@@ -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,11 +29,13 @@ | |||||
#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" | |||||
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); | ||||