Summer Training Warm-up Competition (Zheng Da)

Posted by alex.hughson on Mon, 08 Jul 2019 20:14:37 +0200

The first few questions are too simple to skip.

Question F: String inversion

Topic Description

Little C likes to write words backwards. Now give you a line of text in lowercase C. Can you reverse every word and output them?

input

The input contains multiple sets of test samples. The first action is an integer T, representing the number of test cases, followed by T test cases.
Each test sample takes up one line and contains more than one word. A line has a maximum of 1000 characters.

output

For each test case, you should output the converted text.

sample input

3
olleh !dlrow
I ekil .bulcmca
I evol .mca

sample output

hello world!
I like acmclub.
I love acm.

The first submission shows compilation errors, what ignores the return value of the function (black question mark......)

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4  
 5 using namespace std;
 6  
 7 void change(char str[])
 8 {
 9     int i,j;
10     j=0;
11     char string[50];
12     if(strlen(str)==1)
13     {
14         printf("%s",str);
15         return ;
16     }
17     for(i=strlen(str)-1;i>=0;i--)
18     {
19         string[j++]=str[i];
20     }
21     string[j++]=0;
22     printf("%s",string);
23     return ;
24 }
25  
26 int main()
27 {
28     int n;
29     scanf("%d",&n);
30     getchar();
31     while(n--)
32     {
33         char str[1000];
34         gets(str);
35         int len=strlen(str);
36         str[len]=' ';
37         len++;
38         str[len]=0;
39         char tem[50];
40         int j=0;
41         for(int i=0;i<len;i++)
42         {
43             if(str[i]!=' ')
44             {
45                 tem[j++]=str[i];
46             }
47             else if(str[i]==' '&&str[i-1]!=' ')
48             {
49                 tem[j++]=0; 
50                 change(tem);
51                 j=0;
52                 memset(tem,0,sizeof(tem));
53                 if(i!=len-1) printf(" ");
54             }
55             else
56                 printf(" ");
57         }
58         printf("\n");
59     }
60     return 0;
61 }
62 /**************************************************************
63     Problem: 1095
64     User: 201820222
65     Language: C++
66     Result: Compilation error
67 ****************************************************************/

The second time you delete a return from the change, you compile it (again black question mark), but it shows a running error.

Runtime errors, illegal memory access, array crossing, pointer drift, calling disabled system functions, etc.

The guess array is smaller.

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4  
 5 using namespace std;
 6   
 7 void change(char str[])
 8 {
 9     int i,j;
10     j=0;
11     char string[50];
12     for(i=strlen(str)-1;i>=0;i--)
13     {
14         string[j++]=str[i];
15     }
16     string[j++]=0;
17     printf("%s",string);
18     return ;
19 }
20  
21 int main()
22 {
23     int n;
24     scanf("%d",&n);
25     getchar();
26     while(n--)
27     {
28         char str[1000];
29         gets(str);
30         int len=strlen(str);
31         str[len]=' ';
32         len++;
33         str[len]=0;
34         char tem[50];
35         int j=0;
36         for(int i=0;i<len;i++)
37         {
38             if(str[i]!=' ')
39             {
40                 tem[j++]=str[i];
41             }
42             else if(str[i]==' '&&str[i-1]!=' ')
43             {
44                 tem[j++]=0; 
45                 change(tem);
46                 j=0;
47                 memset(tem,0,sizeof(tem));
48                 if(i!=len-1) printf(" ");
49             }
50             else
51                 printf(" ");
52         }
53         printf("\n");
54     }
55     return 0;
56 }
57 /**************************************************************
58     Problem: 1095
59     User: 201820222
60     Language: C++
61     Result: Running error
62 ****************************************************************/

The third time, perfect.

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4  
 5 using namespace std;
 6   
 7 void change(char str[])
 8 {
 9     int i,j;
10     j=0;
11     char string[1001];
12     for(i=strlen(str)-1;i>=0;i--)
13     {
14         string[j++]=str[i];
15     }
16     string[j++]=0;
17     printf("%s",string);
18     return ;
19 }
20  
21 int main()
22 {
23     int n;
24     scanf("%d",&n);
25     getchar();
26     while(n--)
27     {
28         char str[1001];
29         gets(str);
30         int len=strlen(str);
31         str[len]=' ';
32         len++;
33         str[len]=0;
34         char tem[1001];
35         int j=0;
36         for(int i=0;i<len;i++)
37         {
38             if(str[i]!=' ')
39             {
40                 tem[j++]=str[i];
41             }
42             else if(str[i]==' '&&str[i-1]!=' ')
43             {
44                 tem[j++]=0; 
45                 change(tem);
46                 j=0;
47                 memset(tem,0,sizeof(tem));
48                 if(i!=len-1) printf(" ");
49             }
50             else
51                 printf(" ");
52         }
53         printf("\n");
54     }
55     return 0;
56 }
57 /**************************************************************
58     Problem: 1095
59     User: 201820222
60     Language: C++
61     Result: Correct
62     Time:1 ms
63     Memory:1120 kb
64 ****************************************************************/

Although this problem is full of twists and turns, it is very happy that the memory consumption is the shortest.

 

Question H: easy

Topic Description

Input a string and output the longest palindrome substring length of the string
Substring: String fragments that appear continuously in the original string.
(Tencent Interview Question)

input

Only one line, containing a string, not more than 100 lengths

output

Output an integer, the length of the longest palindrome substring

sample input

1abcba2

sample output

 5

Direct Parity, Violent Solution

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4  
 5 using namespace std;
 6  
 7 int main()
 8 {
 9     char str[100];
10     gets(str);
11     int max=0;
12     for(int i=1;i<strlen(str);i++)
13     {
14         int m=1;
15         for(int j=1;;j++)
16         {
17             if(i-j<0||i+j>strlen(str))
18                 break;
19             else if(str[i-j]==str[i+j])
20                 m+=2;
21             else
22                 break;
23         }
24         if(m>max)
25             max=m;
26     }
27     for(int i=1;i<strlen(str)-1;i++)
28     {
29         if(str[i]!=str[i+1])
30             continue;
31         int m=2;
32         for(int j=1;;j++)
33         {
34             if(i-j<0||i+1+j>strlen(str))
35                 break;
36             else if(str[i-j]==str[i+1+j])
37                 m+=2;
38             else
39                 break;
40         }
41         if(m>max)
42             max=m;
43     }
44     printf("%d\n",max);
45     return 0;
46 }

 

You can also set i, j, traverse from both ends, use reverse, exchange strings between I and j, store them in a temporary array, and then see if the temporary array and the original string are equal, when the same j-i+1 should be the largest.

 

Question I: Longest Common Subsequence

Topic Description

Give you a sequence X and another sequence Z. When all the elements in Z exist in X and the subscript order in X is strictly incremental, then Z is called a subsequence of X.
For example, Z=<a, b, f, c> is a subsequence of sequence X=<a, b, c, f, b, c>, and the subscript sequence of elements in Z in X is <1, 2, 4, 6>.
Here are two sequences X and Y. What is the length of their longest common subsequence?

input

The input contains multiple sets of test data. Each set of inputs takes up one line and is two strings separated by several spaces. The length of each string does not exceed 100.

output

For each set of inputs, the length of the longest common subsequence of two strings is output.

sample input

abcfbc abfcab
programming contest 
abcd mnp

sample output

4
2
0

This classic DP, I've seen it before, but I've almost forgotten it. Let's keep it. When reviewing DP next week, I'll watch it.

 

 

Question J: Limits of string statistics

Topic Description

Give a string, consisting of only uppercase and lowercase letters, in which the uppercase letters are picked out in the original order to form a new string and output the string.  

input

One line, string. 1=<Length<=10000000

output

A string of pure capitals

sample input

AAaaBBbb

sample output

AABB

That's right, but the details aren't ready. Let's go ahead and look at it. Ouch.

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4  
 5 using namespace std;
 6  
 7 int main()
 8 {
 9     char c;10     c=getchar();
11     while(c!='\n')
12     {
13         if(c>='A'&&c<='Z')
14         printf("%c",c);
15         c=getchar();
16     }
17     printf("\n");
18     return 0;
19 }
20 /**************************************************************
21     Problem: 4388
22     User: 201820222
23     Language: C++
24     Result: Overtime
25 ****************************************************************/

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4  
 5 using namespace std;
 6  
 7 int main()
 8 {
 9     char c;
10     while(~scanf("%c",&c))
11     {
12         if(c>='A'&&c<='Z')
13         printf("%c",c);
14     }
15     printf("\n");
16     return 0;
17 }
18 /**************************************************************
19     Problem: 4388
20     User: 201820222
21     Language: C++
22     Result: Overtime
23 ****************************************************************/

The most interesting thing is the following, the same code, different results...

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4  
 5 using namespace std;
 6  
 7 int main()
 8 {
 9     char ch;
10     while((ch=getchar())!=EOF&&ch!='\n')
11     {
12         if(ch>='A'&&ch<='Z')
13             printf("%c",ch);
14     }
15     printf("\n");
16     return 0;
17 }
18 /**************************************************************
19     Problem: 4388
20     User: 201820222
21     Language: C++
22     Result: Compilation error
23 ****************************************************************/

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4   
 5 using namespace std;
 6   
 7 int main()
 8 {
 9     char ch;
10     while((ch=getchar())!=EOF&&ch!='\n')
11     {
12         if(ch>='A'&&ch<='Z')
13             printf("%c",ch);
14     }
15     printf("\n");
16     return 0;
17 }
18 /**************************************************************
19     Problem: 4388
20     User: 201820222
21     Language: C++
22     Result: Correct
23     Time:593 ms
24     Memory:1120 kb
25 ****************************************************************/

Topics: PHP Programming