Skip to content

Commit c315dad

Browse files
author
Your Name
committed
[hcheng] #zju 3818 pretty poety.
0 parents  commit c315dad

File tree

7 files changed

+367
-0
lines changed

7 files changed

+367
-0
lines changed

.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Debug
2+
Debug/*.*
3+
ipch/*
4+
ipch
5+
Algorithm/*Debug*
6+
Algorithm/*Bin*
7+
Algorithm/*ipch*
8+
*.sdf
9+
10+
*.DS_Store
11+
*.obj
12+
*.pdb
13+
*.suo
14+
*.user
15+
*.cache
16+
*.orig
17+
*.iws
18+
*.log
19+
*.pyc

Algorithm.sln

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Algorithm", "Algorithm\Algorithm.vcxproj", "{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Win32 = Debug|Win32
9+
Release|Win32 = Release|Win32
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}.Debug|Win32.ActiveCfg = Debug|Win32
13+
{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}.Debug|Win32.Build.0 = Debug|Win32
14+
{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}.Release|Win32.ActiveCfg = Release|Win32
15+
{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}.Release|Win32.Build.0 = Release|Win32
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

Algorithm/Algorithm.vcxproj

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup Label="ProjectConfigurations">
4+
<ProjectConfiguration Include="Debug|Win32">
5+
<Configuration>Debug</Configuration>
6+
<Platform>Win32</Platform>
7+
</ProjectConfiguration>
8+
<ProjectConfiguration Include="Release|Win32">
9+
<Configuration>Release</Configuration>
10+
<Platform>Win32</Platform>
11+
</ProjectConfiguration>
12+
</ItemGroup>
13+
<PropertyGroup Label="Globals">
14+
<ProjectGuid>{C11C2B7D-DF91-42CF-A0D0-E1462D4759A5}</ProjectGuid>
15+
<RootNamespace>Algorithm</RootNamespace>
16+
</PropertyGroup>
17+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
18+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
19+
<ConfigurationType>Application</ConfigurationType>
20+
<UseDebugLibraries>true</UseDebugLibraries>
21+
<CharacterSet>MultiByte</CharacterSet>
22+
</PropertyGroup>
23+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
24+
<ConfigurationType>Application</ConfigurationType>
25+
<UseDebugLibraries>false</UseDebugLibraries>
26+
<WholeProgramOptimization>true</WholeProgramOptimization>
27+
<CharacterSet>MultiByte</CharacterSet>
28+
</PropertyGroup>
29+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
30+
<ImportGroup Label="ExtensionSettings">
31+
</ImportGroup>
32+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
33+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
34+
</ImportGroup>
35+
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
36+
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
37+
</ImportGroup>
38+
<PropertyGroup Label="UserMacros" />
39+
<PropertyGroup />
40+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
41+
<ClCompile>
42+
<WarningLevel>Level3</WarningLevel>
43+
<Optimization>Disabled</Optimization>
44+
</ClCompile>
45+
<Link>
46+
<GenerateDebugInformation>true</GenerateDebugInformation>
47+
</Link>
48+
</ItemDefinitionGroup>
49+
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
50+
<ClCompile>
51+
<WarningLevel>Level3</WarningLevel>
52+
<Optimization>MaxSpeed</Optimization>
53+
<FunctionLevelLinking>true</FunctionLevelLinking>
54+
<IntrinsicFunctions>true</IntrinsicFunctions>
55+
</ClCompile>
56+
<Link>
57+
<GenerateDebugInformation>true</GenerateDebugInformation>
58+
<EnableCOMDATFolding>true</EnableCOMDATFolding>
59+
<OptimizeReferences>true</OptimizeReferences>
60+
</Link>
61+
</ItemDefinitionGroup>
62+
<ItemGroup>
63+
<ClCompile Include="zju3818.cpp" />
64+
</ItemGroup>
65+
<ItemGroup>
66+
<None Include="input.txt" />
67+
<None Include="ReadMe.txt" />
68+
</ItemGroup>
69+
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
70+
<ImportGroup Label="ExtensionTargets">
71+
</ImportGroup>
72+
</Project>

Algorithm/Algorithm.vcxproj.filters

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<ItemGroup>
4+
<Filter Include="Source Files">
5+
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
6+
<Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
7+
</Filter>
8+
<Filter Include="Header Files">
9+
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
10+
<Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
11+
</Filter>
12+
<Filter Include="Resource Files">
13+
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
14+
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
15+
</Filter>
16+
<Filter Include="Source Files\zij3818">
17+
<UniqueIdentifier>{ec31a59d-a1fe-4533-b56a-bba9cff937b5}</UniqueIdentifier>
18+
</Filter>
19+
</ItemGroup>
20+
<ItemGroup>
21+
<None Include="input.txt">
22+
<Filter>Source Files\zij3818</Filter>
23+
</None>
24+
<None Include="ReadMe.txt">
25+
<Filter>Source Files\zij3818</Filter>
26+
</None>
27+
</ItemGroup>
28+
<ItemGroup>
29+
<ClCompile Include="zju3818.cpp">
30+
<Filter>Source Files\zij3818</Filter>
31+
</ClCompile>
32+
</ItemGroup>
33+
</Project>

Algorithm/ReadMe.txt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
http://stackoverflow.com/questions/12885628/changing-the-scanf-delimiter
2+
3+
######################<<Changing the scanf() delimiter>>###################################
4+
5+
Consider this (C99) code:
6+
7+
#include <stdio.h>
8+
9+
int main(void)
10+
{
11+
char buffer[256];
12+
13+
while (scanf("%255[^\n]", buffer) == 1)
14+
printf("Found <<%s>>\n", buffer);
15+
int c;
16+
if ((c = getchar()) != EOF)
17+
printf("Failed on character %d (%c)\n", c, c);
18+
return(0);
19+
}
20+
When I run it and type in a string 'absolutely anything with spaces TABTABtabs galore!', it gives me:
21+
22+
Found <<absolutely anything with spaces tabs galore!>>
23+
Failed on character 10 (
24+
)
25+
ASCII (UTF-8) 1010 is newline, of course.
26+
27+
Does this help you understand your problem?
28+
29+
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?
30+
31+
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.
32+
33+
#include <stdio.h>
34+
35+
int main(void)
36+
{
37+
char line[256];
38+
char sen[256];
39+
40+
while (fgets(line, sizeof(line), stdin) != 0)
41+
{
42+
if (sscanf(line, "%255[^\n]", sen) != 1)
43+
break;
44+
printf("Found <<%s>>\n", sen);
45+
}
46+
int c;
47+
if ((c = getchar()) != EOF)
48+
printf("Failed on character %d (%c)\n", c, c);
49+
return(0);
50+
}
51+
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.
52+
53+
char sen[20];
54+
for (i=0;i<2;i++)
55+
{
56+
scanf("%[^\n]s",sen);
57+
printf("%s\n",sen);
58+
}
59+
Problems:
60+
61+
You do not check whether scanf() succeeded.
62+
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.
63+
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.
64+
Return value from scanf()
65+
66+
The definition of scanf() (from ISO/IEC 9899:1999) is:
67+
68+
�7.19.6.4 The scanf function
69+
70+
Synopsis
71+
72+
#include <stdio.h>
73+
int scanf(const char * restrict format, ...);
74+
Description
75+
76+
2 The scanf function is equivalent to fscanf with the argument stdin interposed before the arguments to scanf.
77+
78+
Returns
79+
80+
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.
81+
82+
Note that when the loop in my first program exits, it is because scanf() returned 0, not EOF.

Algorithm/input.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4
2+
niconiconi~
3+
pettan,pettan,tsurupettan
4+
wafuwafu
5+
huhuhuhu

Algorithm/zju3818.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<string.h>
4+
#include <ctype.h>
5+
6+
int IsSchemaOfABABA(char * str){
7+
if(str == NULL || strlen(str) < 4)
8+
return false;
9+
int lenOfStr = strlen(str);
10+
int lenOfA = 1;
11+
int lenOfB = 1;
12+
int mod2OfStr = lenOfStr % 2;
13+
for(lenOfA = 1; lenOfA < lenOfStr / 2; lenOfA++)
14+
{
15+
//should meet the constrains about the relationship among lenOfA, lenOfB, lenOfStr
16+
if(lenOfA % 2 == mod2OfStr && (lenOfStr - 3 * lenOfA) % 2 == 0 && (lenOfB = (lenOfStr - 3 * lenOfA) / 2) >= 1)
17+
{
18+
int lenOfAB = lenOfA + lenOfB;
19+
20+
int inc = 0;
21+
while(inc < lenOfA && str[inc] == str[lenOfAB + inc]) inc++;
22+
if(inc < lenOfA) continue;
23+
24+
inc = 0;
25+
while(inc < lenOfA && str[inc] == str[lenOfAB * 2 + inc]) inc++;
26+
if(inc < lenOfA) continue;
27+
28+
inc = 0;
29+
while(inc < lenOfB && str[lenOfA + inc] == str[2 * lenOfA + lenOfB + inc]) inc++;
30+
if(inc < lenOfB) continue;
31+
32+
//A and B should not be same
33+
if(lenOfA == lenOfB){
34+
inc = 0;
35+
while(inc < lenOfA && str[inc] == str[inc + lenOfA])inc++;
36+
if(inc == lenOfA)
37+
continue;
38+
}
39+
40+
return true;
41+
}
42+
}
43+
return false;
44+
}
45+
46+
int IsSchemaOfABABCAB(char * str){
47+
if(str == NULL || strlen(str) < 6)
48+
return false;
49+
int lenOfStr = strlen(str);
50+
int lenOfC = 1;
51+
int mod3OfStr = lenOfStr % 3;
52+
int lenOfAB = 1;
53+
int lenOfA = 1;
54+
int lenOfB = 1;
55+
for(lenOfC = 1; lenOfC <= lenOfStr - 6; lenOfC ++)
56+
{
57+
//should meet the constrains of schema of ABABCAB
58+
if(lenOfC % 3 == mod3OfStr && (lenOfStr - lenOfC) % 3 == 0 && (lenOfAB = (lenOfStr - lenOfC) / 3) >= 2)
59+
{
60+
int inc = 0;
61+
while(inc < lenOfAB && str[inc] == str[lenOfAB + inc]) inc++;
62+
if(inc < lenOfAB) continue;
63+
64+
inc = 0;
65+
while(inc < lenOfAB && str[inc] == str[lenOfAB * 2 + lenOfC + inc]) inc++;
66+
if(inc < lenOfAB) continue;
67+
68+
69+
for(lenOfA = 1; lenOfA < lenOfAB;lenOfA++)
70+
{
71+
lenOfB = lenOfAB - lenOfA;
72+
if(lenOfA == lenOfB)
73+
{
74+
inc = 0;
75+
while(inc < lenOfA && str[inc] == str[lenOfA + inc]) inc++;
76+
if(inc == lenOfA) continue;
77+
}
78+
79+
if(lenOfA == lenOfC)
80+
{
81+
inc = 0;
82+
while(inc < lenOfA && str[inc] == str[lenOfAB * 2 + inc]) inc++;
83+
if(inc == lenOfA) continue;
84+
}
85+
86+
if(lenOfB == lenOfC)
87+
{
88+
inc = 0;
89+
while(inc < lenOfB && str[lenOfA + inc] == str[lenOfAB * 2 + inc]) inc++;
90+
if(inc == lenOfB) continue;
91+
}
92+
return true;
93+
}
94+
}
95+
}
96+
return false;
97+
}
98+
99+
void SpiltPunc(char *src, char *dest)
100+
{
101+
if(src == NULL || dest == NULL)
102+
return;
103+
int inc = 0;
104+
int lenOfSrc = strlen(src);
105+
int indexOfDest = 0;;
106+
while(inc < lenOfSrc)
107+
{
108+
if(isalpha(src[inc]))
109+
{
110+
dest[indexOfDest++] = src[inc];
111+
}
112+
inc++;
113+
}
114+
dest[indexOfDest] = '\0';
115+
}
116+
117+
int main()
118+
{
119+
const int LEN_MAX = 55;
120+
int T = 0;
121+
scanf("%d%*c", &T);
122+
char str[LEN_MAX]={'\0'};
123+
char strOfNoPunc[LEN_MAX] = {'\0'};
124+
int lineOfInput = 0;
125+
while(lineOfInput < T)
126+
{
127+
scanf("%s", str);
128+
129+
SpiltPunc(str, strOfNoPunc);
130+
if(IsSchemaOfABABA(strOfNoPunc) || IsSchemaOfABABCAB(strOfNoPunc))
131+
printf("Yes\n");
132+
else
133+
printf("No\n");
134+
lineOfInput++;
135+
}
136+
}

0 commit comments

Comments
 (0)