Skip to content

Commit

Permalink
[hcheng] #zju 3818 pretty poety.
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Sep 14, 2014
0 parents commit c315dad
Show file tree
Hide file tree
Showing 7 changed files with 367 additions and 0 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Debug
Debug/*.*
ipch/*
ipch
Algorithm/*Debug*
Algorithm/*Bin*
Algorithm/*ipch*
*.sdf

*.DS_Store
*.obj
*.pdb
*.suo
*.user
*.cache
*.orig
*.iws
*.log
*.pyc
20 changes: 20 additions & 0 deletions Algorithm.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Algorithm", "Algorithm\Algorithm.vcxproj", "{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}.Debug|Win32.ActiveCfg = Debug|Win32
{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}.Debug|Win32.Build.0 = Debug|Win32
{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}.Release|Win32.ActiveCfg = Release|Win32
{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
72 changes: 72 additions & 0 deletions Algorithm/Algorithm.vcxproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http:https://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>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}</ProjectGuid>
<RootNamespace>Algorithm</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</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>
<PropertyGroup Label="UserMacros" />
<PropertyGroup />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
</ClCompile>
<Link>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="zju3818.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="input.txt" />
<None Include="ReadMe.txt" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
33 changes: 33 additions & 0 deletions Algorithm/Algorithm.vcxproj.filters
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http:https://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<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>
<Filter Include="Source Files\zij3818">
<UniqueIdentifier>{ec31a59d-a1fe-4533-b56a-bba9cff937b5}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<None Include="input.txt">
<Filter>Source Files\zij3818</Filter>
</None>
<None Include="ReadMe.txt">
<Filter>Source Files\zij3818</Filter>
</None>
</ItemGroup>
<ItemGroup>
<ClCompile Include="zju3818.cpp">
<Filter>Source Files\zij3818</Filter>
</ClCompile>
</ItemGroup>
</Project>
82 changes: 82 additions & 0 deletions Algorithm/ReadMe.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
http:https://stackoverflow.com/questions/12885628/changing-the-scanf-delimiter

######################<<Changing the scanf() delimiter>>###################################

Consider this (C99) code:

#include <stdio.h>

int main(void)
{
char buffer[256];

while (scanf("%255[^\n]", buffer) == 1)
printf("Found <<%s>>\n", buffer);
int c;
if ((c = getchar()) != EOF)
printf("Failed on character %d (%c)\n", c, c);
return(0);
}
When I run it and type in a string 'absolutely anything with spaces TABTABtabs galore!', it gives me:

Found <<absolutely anything with spaces tabs galore!>>
Failed on character 10 (
)
ASCII (UTF-8) 1010 is newline, of course.

Does this help you understand your problem?

It works in this case (for a single line) but if I want to take multiple lines of input into an array of arrays then it fails. And I don't get how scanf returns a value in your code?

There are reasons why many (most?) experienced C programmers avoid scanf() and fscanf() like the plague; they're too hard to get to work correctly. I'd recommend this alternative, using sscanf(), which does not get the same execration that scanf() and fscanf() do.

#include <stdio.h>

int main(void)
{
char line[256];
char sen[256];

while (fgets(line, sizeof(line), stdin) != 0)
{
if (sscanf(line, "%255[^\n]", sen) != 1)
break;
printf("Found <<%s>>\n", sen);
}
int c;
if ((c = getchar()) != EOF)
printf("Failed on character %d (%c)\n", c, c);
return(0);
}
This reads the line of input (using fgets() which ensures no buffer overflow (pretend that the gets() function, if you've heard of it, melts your computer to a pool of metal and silicon), then uses sscanf() to process that line. This deals with newlines, which are the downfall of the original code.

char sen[20];
for (i=0;i<2;i++)
{
scanf("%[^\n]s",sen);
printf("%s\n",sen);
}
Problems:

You do not check whether scanf() succeeded.
You leave the newline in the buffer on the first iteration; the second iteration generates a return value of 0 because the first character to read is newline, which is the character excluded by the scan set.
The gibberish you see is likely the first line of input, repeated. Indeed, if it were not for the bounded loop, it would not wait for you to type anything more; it would spit out the first line over and over again.
Return value from scanf()

The definition of scanf() (from ISO/IEC 9899:1999) is:

�7.19.6.4 The scanf function

Synopsis

#include <stdio.h>
int scanf(const char * restrict format, ...);
Description

2 The scanf function is equivalent to fscanf with the argument stdin interposed before the arguments to scanf.

Returns

3 The scanf function returns the value of the macro EOF if an input failure occurs before any conversion. Otherwise, the scanf function returns the number of input items assigned, which can be fewer than provided for, or even zero, in the event of an early matching failure.

Note that when the loop in my first program exits, it is because scanf() returned 0, not EOF.
5 changes: 5 additions & 0 deletions Algorithm/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4
niconiconi~
pettan,pettan,tsurupettan
wafuwafu
huhuhuhu
136 changes: 136 additions & 0 deletions Algorithm/zju3818.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <ctype.h>

int IsSchemaOfABABA(char * str){
if(str == NULL || strlen(str) < 4)
return false;
int lenOfStr = strlen(str);
int lenOfA = 1;
int lenOfB = 1;
int mod2OfStr = lenOfStr % 2;
for(lenOfA = 1; lenOfA < lenOfStr / 2; lenOfA++)
{
//should meet the constrains about the relationship among lenOfA, lenOfB, lenOfStr
if(lenOfA % 2 == mod2OfStr && (lenOfStr - 3 * lenOfA) % 2 == 0 && (lenOfB = (lenOfStr - 3 * lenOfA) / 2) >= 1)
{
int lenOfAB = lenOfA + lenOfB;

int inc = 0;
while(inc < lenOfA && str[inc] == str[lenOfAB + inc]) inc++;
if(inc < lenOfA) continue;

inc = 0;
while(inc < lenOfA && str[inc] == str[lenOfAB * 2 + inc]) inc++;
if(inc < lenOfA) continue;

inc = 0;
while(inc < lenOfB && str[lenOfA + inc] == str[2 * lenOfA + lenOfB + inc]) inc++;
if(inc < lenOfB) continue;

//A and B should not be same
if(lenOfA == lenOfB){
inc = 0;
while(inc < lenOfA && str[inc] == str[inc + lenOfA])inc++;
if(inc == lenOfA)
continue;
}

return true;
}
}
return false;
}

int IsSchemaOfABABCAB(char * str){
if(str == NULL || strlen(str) < 6)
return false;
int lenOfStr = strlen(str);
int lenOfC = 1;
int mod3OfStr = lenOfStr % 3;
int lenOfAB = 1;
int lenOfA = 1;
int lenOfB = 1;
for(lenOfC = 1; lenOfC <= lenOfStr - 6; lenOfC ++)
{
//should meet the constrains of schema of ABABCAB
if(lenOfC % 3 == mod3OfStr && (lenOfStr - lenOfC) % 3 == 0 && (lenOfAB = (lenOfStr - lenOfC) / 3) >= 2)
{
int inc = 0;
while(inc < lenOfAB && str[inc] == str[lenOfAB + inc]) inc++;
if(inc < lenOfAB) continue;

inc = 0;
while(inc < lenOfAB && str[inc] == str[lenOfAB * 2 + lenOfC + inc]) inc++;
if(inc < lenOfAB) continue;


for(lenOfA = 1; lenOfA < lenOfAB;lenOfA++)
{
lenOfB = lenOfAB - lenOfA;
if(lenOfA == lenOfB)
{
inc = 0;
while(inc < lenOfA && str[inc] == str[lenOfA + inc]) inc++;
if(inc == lenOfA) continue;
}

if(lenOfA == lenOfC)
{
inc = 0;
while(inc < lenOfA && str[inc] == str[lenOfAB * 2 + inc]) inc++;
if(inc == lenOfA) continue;
}

if(lenOfB == lenOfC)
{
inc = 0;
while(inc < lenOfB && str[lenOfA + inc] == str[lenOfAB * 2 + inc]) inc++;
if(inc == lenOfB) continue;
}
return true;
}
}
}
return false;
}

void SpiltPunc(char *src, char *dest)
{
if(src == NULL || dest == NULL)
return;
int inc = 0;
int lenOfSrc = strlen(src);
int indexOfDest = 0;;
while(inc < lenOfSrc)
{
if(isalpha(src[inc]))
{
dest[indexOfDest++] = src[inc];
}
inc++;
}
dest[indexOfDest] = '\0';
}

int main()
{
const int LEN_MAX = 55;
int T = 0;
scanf("%d%*c", &T);
char str[LEN_MAX]={'\0'};
char strOfNoPunc[LEN_MAX] = {'\0'};
int lineOfInput = 0;
while(lineOfInput < T)
{
scanf("%s", str);

SpiltPunc(str, strOfNoPunc);
if(IsSchemaOfABABA(strOfNoPunc) || IsSchemaOfABABCAB(strOfNoPunc))
printf("Yes\n");
else
printf("No\n");
lineOfInput++;
}
}

0 comments on commit c315dad

Please sign in to comment.