HHTC_学校集训编程题目(8)(个人赛_2)

HHTC_学校集训编程题目(8)(个人赛_2)


今天的个人赛有点东西~~
首先有三个题目是之前写过的,就不再多说了
然后,有些题目是自己写的,也有听别人讲解后写的
上午比赛,下午讲题,时间就是过的快

D - Code Formatter

Some companies have special requirements for source code format, and it is also good for programmers to keep consistent code style. You are asked to write a simple code formatter for the company to help the poor programmers.

The first thing you need to do is to check whether the source code contains tabs (represented as the escape character ‘\t’), since different terminals have different ways to display tabs, it’s better not to use them, but replace them with spaces. The code formatter should replace each tab of the source code with 4(four) blank spaces.

Then you need to remove trailing spaces of the source file. Trailing spaces are one or more consecutive whitespaces right before the EOL (end of line, represented as the escape character ‘\n’), and they usually have no meaning in most programming language, so they can be safely removed.

Input

The input contains multiple test cases!

The first line is an integer N indicating the number of test cases. Each test case is the source which contains not more than 100 lines given to you to format. A single line containing only “##” marks the end of a test case.

Output

For each test case, output a log of the formatter in two lines of the following format:

#A tab(s) replaced #B trailing space(s) removed Where #A is the number of tabs replaced and #B is the number of trailing spaces removed.

Sample Input

2
include <stdio.h>

int main()
{
	int a,b;
	while(scanf("%d %d",&a, &b) != EOF) 
		printf("%d\n",a+b);             
        
}
##

##

Sample Output

4 tab(s) replaced
22 trailing space(s) removed
0 tab(s) replaced
0 trailing space(s) removed
Note

In order to show the whitespaces precisely, all the characters in sample input are underlined. They are not the underscore character.

就是统计空格和tab的个数,只不过有一个坑点
注意一下就好了
AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
int ans[102][11];

int main()
{
    int T;
    cin>>T;
    while(T--){
        string s;
        int tab = 0,space = 0;
        while(1){
            getline(cin,s);
            if(s == "##")
                break;
            for(int i=0;i<s.size();i++){
                if(s[i] == '\t'){
                    tab++;
                }
            }
            for(int i=s.size()-1;i>=0;i--){
                if(s[i] == '\t')
                    space += 4;
                else if(s[i] == ' ')
                    space++;
                else
                    break;
            }
        }
        cout<<tab<<" tab(s) replaced"<<endl;
        cout<<space<<" trailing space(s) removed"<<endl;
    }
    return 0;
}

E - To the Max

Given a two-dimensional array of positive and negative integers, a sub-rectangle is any contiguous sub-array of size 1*1 or greater located within the whole array. The sum of a rectangle is the sum of all the elements in that rectangle. In this problem the sub-rectangle with the largest sum is referred to as the maximal sub-rectangle.
As an example, the maximal sub-rectangle of the array:
0 -2 -7 0
9 2 -6 2
-4 1 -4 1
-1 8 0 -2
is in the lower left corner:
9 2
-4 1
-1 8
and has a sum of 15.

Input

The input consists of an N * N array of integers. The input begins with a single positive integer N on a line by itself, indicating the size of the square two-dimensional array. This is followed by N^2 integers separated by whitespace (spaces and newlines). These are the N^2 integers of the array, presented in row-major order. That is, all numbers in the first row, left to right, then all numbers in the second row, left to right, etc. N may be as large as 100. The numbers in the array will be in the range [-127,127].

Output

Output the sum of the maximal sub-rectangle.

Sample Input

4
0 -2 -7 0 9 2 -6 2
-4 1 -4  1 -1

8  0 -2

Sample Output

15

就是求子矩阵中加起来和最大的
利用前缀和
AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

int n;

int main()
{
    while(cin>>n){
        int a[130][130],sum;
        for(int i=0;i<n;i++){
            sum = 0;
            for(int j=0;j<n;j++){
                int x;
                cin>>x;
                a[i][j] = a[i-1][j] + x;
            }
        }
        int maxx = 0;
        for(int i=0;i<n;i++){
            for(int j=i;j<n;j++){
                sum = 0;
                for(int k=0;k<n;k++){
                    sum += a[j][k]-a[i][k];
                    if(sum < 0)
                        sum = 0;
                    maxx = max(maxx,sum);
                }
            }
        }
        cout<<maxx<<endl;
    }
    return 0;
}

G - Zipper

Given three strings, you are to determine whether the third string can be formed by combining the characters in the first two strings. The first two strings can be mixed arbitrarily, but each must stay in its original order.

For example, consider forming “tcraete” from “cat” and “tree”:

String A: cat
String B: tree
String C: tcraete

As you can see, we can form the third string by alternating characters from the two strings. As a second example, consider forming “catrtee” from “cat” and “tree”:

String A: cat
String B: tree
String C: catrtee

Finally, notice that it is impossible to form “cttaree” from “cat” and “tree”.

Input

The first line of input contains a single positive integer from 1 through 1000. It represents the number of data sets to follow. The processing for each data set is identical. The data sets appear on the following lines, one data set per line.

For each data set, the line of input consists of three strings, separated by a single space. All strings are composed of upper and lower case letters only. The length of the third string is always the sum of the lengths of the first two strings. The first two strings will have lengths between 1 and 200 characters, inclusive.

Output

For each data set, print:
Data set n: yes
if the third string can be formed from the first two, or
Data set n: no
if it cannot. Of course n should be replaced by the data set number. See the sample output below for an example.

Sample Input

3
cat tree tcraete
cat tree catrtee
cat tree cttaree

Sample Output

Data set 1: yes
Data set 2: yes
Data set 3: no

就是判断第三个字符串是不是由前面两个字符串组合而成的
而且前两个字符串自身的顺序不能乱
因为只有三个字符串,直接使用dfs进行搜索,两边开展
AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;

string a,b,c;
bool flag,vis[205][205];

void dfs(int x,int y,int z){	//分别表示三个字符串的下标
    if(flag)
        return;
    if(z == c.size()){
        flag = true;
        cout<<"yes"<<endl;
        return ;
    }
    if(vis[x][y] == 1)	//标记,防止循环,只找一次
        return ;
    vis[x][y] = 1;
    if(a[x] == c[z])
        dfs(x+1,y,z+1);
    if(b[y] == c[z])
        dfs(x,y+1,z+1);
}

int main()
{
    int T,k=1;
    cin>>T;
    while(T--){
        memset(vis,0,sizeof(vis));
        cin>>a>>b>>c;
        cout<<"Data set "<<k++<<": ";
        if(a.size() + b.size() != c.size()){
            cout<<"no"<<endl;
            break;
        }
        flag = false;
        dfs(0,0,0);
        if( !flag )
            cout<<"no"<<endl;
    }
    return 0;
}

I - Tempter of the Bone

The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.

The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.

Input

The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:

‘X’: a block of wall, which the doggie cannot enter;
‘S’: the start point of the doggie;
‘D’: the Door; or
‘.’: an empty block.

The input is terminated with three 0’s. This test case is not to be processed.

Output

For each test case, print in one line “YES” if the doggie can survive, or “NO” otherwise.

Sample Input

4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0

Sample Output

NO
YES

毋庸置疑就是经典的搜索题目,只不过注意下剪枝就好了
我自己写的代码超市了,,,,
不过听了别人的说法,用了一个搜索的小技巧,奇偶剪枝
不明白的可以百度一下,不过我有些同学没用奇偶剪枝也过了~~
可能是我考虑的情况不够吧
奇偶剪枝:

int ans = k - time - abs(x - x2) - abs(y - y2);
if(ans < 0 || ans & 1)
	 return ;

AC代码:

#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <map>
#include <set>
#include <math.h>
#include <vector>
#include <algorithm>
using namespace std;

int n,m,k,x1,x2,y3,y2;
bool flag;
int d[4][2] = {1,0,0,1,-1,0,0,-1};
char a[15][15];

void dfs(int x,int y,int time){
    if(x == x2 && y == y2 && time ==k)
        flag = true;
    if(flag)
        return ;
    int ans = k - time - abs(x - x2) - abs(y - y2);
    if(ans < 0 || ans & 1)
        return ;
    if(x<0 || y<0 || x>=n || y>=m)
        return ;
    for(int i=0;i<4;i++){
        int xx = x + d[i][0];
        int yy = y + d[i][1];
        if(a[xx][yy] != 'X'){
            a[xx][yy] = 'X';
            dfs(xx,yy,time+1);
            a[xx][yy] = '.';
        }
    }
}

int main()
{
    while(scanf("%d %d %d",&n,&m,&k) && (n!=0||m!=0||k!=0)){
        int num = 0;
        getchar();
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                scanf("%c",&a[i][j]);
                if(a[i][j] == 'S'){
                    x1 = i;
                    y3 = j;
                }
                if(a[i][j] == 'D'){
                    x2 = i;
                    y2 = j;
                }
                if(a[i][j] == 'X')
                    num++;
            }
            getchar();
        }
        if(n*m - num < k){
            printf("NO\n");
            continue;
        }
        flag = false;
        a[x1][y3] = 'X';
        dfs(x1,y3,0);
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值