Skip to content

Instantly share code, notes, and snippets.

@WindAzure
Last active September 16, 2018 16:23
Show Gist options
  • Save WindAzure/3294439806e5081753bde20d39a88ed0 to your computer and use it in GitHub Desktop.
Save WindAzure/3294439806e5081753bde20d39a88ed0 to your computer and use it in GitHub Desktop.
Uva 1588
#include <stdio.h>
#include <string.h>
#include <algorithm>
auto masterLen = 0, drivenLen = 0;
int masterSection[101], drivenSection[101];
bool IsDriftingDrivenSectionMatchMasterSection(int offset)
{
for (auto i = 0; i < drivenLen; i++)
{
auto masterPos = i + offset;
if (0 <= masterPos && masterPos < masterLen && drivenSection[i] + masterSection[masterPos] > 3)
{
return false;
}
}
return true;
}
void MatchInMiddle(int &result)
{
auto len = masterLen - drivenLen;
for (auto offset = 0; offset <= len; offset++)
{
if (IsDriftingDrivenSectionMatchMasterSection(offset))
{
result = masterLen;
return;
}
}
}
void MatchInForward(int &result)
{
for (auto offset = -drivenLen + 1; offset < 0; offset++)
{
if (IsDriftingDrivenSectionMatchMasterSection(offset))
{
result = std::min(result, masterLen - offset);
}
}
}
void MatchInBackward(int &result)
{
for (auto offset = masterLen - drivenLen + 1; offset < masterLen; offset++)
{
if (IsDriftingDrivenSectionMatchMasterSection(offset))
{
result = std::min(result, drivenLen + offset);
}
}
}
int GetMinStripeLength()
{
auto result = masterLen + drivenLen;
MatchInMiddle(result);
MatchInForward(result);
MatchInBackward(result);
return result;
}
void ConvertStrIntoIntInArray(char strArray[], int intArray[])
{
memset(intArray, sizeof(int) * 101, 0);
auto len = strlen(strArray);
for (auto i = 0; i < len; i++)
{
intArray[i] = strArray[i] - '0';
}
}
void ConvertInputData(char strMasterSection[], char strDrivenSection[])
{
masterLen = strlen(strMasterSection);
drivenLen = strlen(strDrivenSection);
ConvertStrIntoIntInArray(strMasterSection, masterSection);
ConvertStrIntoIntInArray(strDrivenSection, drivenSection);
if (masterLen < drivenLen)
{
std::swap(masterLen, drivenLen);
for (auto i = 0; i < 101; i++)
{
std::swap(masterSection[i], drivenSection[i]);
}
}
}
int main()
{
char strMasterSection[101], strDrivenSection[101];
while (~scanf("%s%s", strMasterSection, strDrivenSection))
{
ConvertInputData(strMasterSection, strDrivenSection);
printf("%d\n", GetMinStripeLength());
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment