Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Homework №11/Knuth–Morris–Pratt/Knuth–Morris–Pratt.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31410.357
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Knuth–Morris–Pratt", "Knuth–Morris–Pratt\Knuth–Morris–Pratt.vcxproj", "{5C13E66F-7004-4B91-82CA-D62CAB4C2071}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5C13E66F-7004-4B91-82CA-D62CAB4C2071}.Debug|x64.ActiveCfg = Debug|x64
{5C13E66F-7004-4B91-82CA-D62CAB4C2071}.Debug|x64.Build.0 = Debug|x64
{5C13E66F-7004-4B91-82CA-D62CAB4C2071}.Debug|x86.ActiveCfg = Debug|Win32
{5C13E66F-7004-4B91-82CA-D62CAB4C2071}.Debug|x86.Build.0 = Debug|Win32
{5C13E66F-7004-4B91-82CA-D62CAB4C2071}.Release|x64.ActiveCfg = Release|x64
{5C13E66F-7004-4B91-82CA-D62CAB4C2071}.Release|x64.Build.0 = Release|x64
{5C13E66F-7004-4B91-82CA-D62CAB4C2071}.Release|x86.ActiveCfg = Release|Win32
{5C13E66F-7004-4B91-82CA-D62CAB4C2071}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F66AE844-2478-4972-BBC9-7D9AFC17B3F7}
EndGlobalSection
EndGlobal
66 changes: 66 additions & 0 deletions Homework №11/Knuth–Morris–Pratt/Knuth–Morris–Pratt/KMP.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include "KMP.h"
#include <malloc.h>
#include <string.h>

int* findPrefix(char* substring, int* error)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const?

{
int* prefix = calloc(strlen(substring) + 1, sizeof(int));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Табуляции вместо пробелов для отступа, не по стайлгайду

if (prefix == NULL)
{
*error = 1;
return NULL;
}
prefix[0] = 0;
int i = 1;
int j = 0;
while (substring[i] != '\0')
{
if (substring[j] == substring[i])
{
prefix[i] = j + 1;
i++;
j++;
}
else if (j == 0)
{
prefix[i] = 0;
i++;
}
else
{
j = prefix[j - 1];
}
}
return prefix;
}

int algorithmKMP(char* string, char* substring, int* prefix)
{
int counterForString = 0;
int CounterForSubstring = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase

while (string[counterForString] != '\0')
{
if (string[counterForString] == substring[CounterForSubstring])
{
counterForString++;
CounterForSubstring++;
if (CounterForSubstring == strlen(substring))
{
return counterForString - strlen(substring) + 1;
}
}
else if (CounterForSubstring == 0)
{
counterForString++;
if (counterForString == strlen(string))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strlen-ы надо заранее посчитать, они работают за линейное время

{
return -1;
}
}
else
{
CounterForSubstring = prefix[CounterForSubstring - 1];
}
}
return -1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

// Function for finding the maximum matching suffix and prefix
int* findPrefix(char* substring, int* error);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кажется, что это просто часть реализации KMP, так что ей нечего делать в заголовочном файле


// Function for searching for a substring in a string
int algorithmKMP(char* string, char* substring, int* prefix);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кто-то из них явно должен быть const

Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>16.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{5c13e66f-7004-4b91-82ca-d62cab4c2071}</ProjectGuid>
<RootNamespace>KnuthMorrisPratt</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v142</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CRT_SECURE_NO_WARNINGS;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="KMP.c" />
<ClCompile Include="Main.c" />
<ClCompile Include="ReadFile.c" />
<ClCompile Include="ReadString.c" />
<ClCompile Include="TestKMP.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="KMP.h" />
<ClInclude Include="ReadFile.h" />
<ClInclude Include="ReadString.h" />
<ClInclude Include="TestKMP.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Исходные файлы">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Файлы заголовков">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Файлы ресурсов">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="ReadFile.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="ReadString.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="KMP.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="Main.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
<ClCompile Include="TestKMP.c">
<Filter>Исходные файлы</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ReadFile.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="ReadString.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="KMP.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
<ClInclude Include="TestKMP.h">
<Filter>Файлы заголовков</Filter>
</ClInclude>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "KMP.h"
#include "ReadString.h"
#include "ReadFile.h"
#include <stdlib.h>
#include <stdio.h>
#include "TestKMP.h"

int main()
{
if (!testKMP())
{
printf("Test failed");
return -1;
}
printf("enter the substring\n");
int error = 0;
char* substring = readString(&error);
if (error == 1)
{
printf("memory not allocated");
return -1;
}
char* string = readFile("Text.txt", &error);
if (error == 1)
{
free(substring);
printf("memory not allocated");
return -1;
}
if (error == 2)
{
free(substring);
printf("file not found");
return -1;
}
int* prefix = findPrefix(substring, &error);
if (error == 1)
{
free(string);
free(substring);
printf("memory not allocated");
return -1;
}
const int result = algorithmKMP(string, substring, prefix);
free(substring);
free(string);
free(prefix);
printf("the position of the first occurrence of the substring in the string (-1 if there is no occurrence) = %d", result);
}
Loading