Codeforces Round #719 (Div. 3)题解(A、B、C)

Codeforces Round #719 (Div. 3)


A. Do Not Be Distracted!

Polycarp has 26 tasks. Each task is designated by a capital letter of the Latin alphabet.

The teacher asked Polycarp to solve tasks in the following way: if Polycarp began to solve some task, then he must solve it to the end, without being distracted by another task. After switching to another task, Polycarp cannot return to the previous task.

Polycarp can only solve one task during the day. Every day he wrote down what task he solved. Now the teacher wants to know if Polycarp followed his advice.

For example, if Polycarp solved tasks in the following order: “DDBBCCCBBEZ”, then the teacher will see that on the third day Polycarp began to solve the task ‘B’, then on the fifth day he got distracted and began to solve the task ‘C’, on the eighth day Polycarp returned to the task ‘B’. Other examples of when the teacher is suspicious: “BAB”, “AABBCCDDEEBZZ” and “AAAAZAAAAA”.

If Polycarp solved the tasks as follows: “FFGZZZY”, then the teacher cannot have any suspicions. Please note that Polycarp is not obligated to solve all tasks. Other examples of when the teacher doesn’t have any suspicious: “BA”, “AFFFCC” and “YYYYY”.

Help Polycarp find out if his teacher might be suspicious.

输入格式

The first line contains an integer t (1≤t≤1000). Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤50) — the number of days during which Polycarp solved tasks.

The second line contains a string of length n, consisting of uppercase Latin letters, which is the order in which Polycarp solved the tasks.

输出格式

For each test case output:

“YES”, if the teacher cannot be suspicious;
“NO”, otherwise.
You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes and YES are all recognized as positive answer).

样例输入

5
3
ABA
11
DDBBCCCBBEZ
7
FFGZZZY
1
Z
2
AB

样例输出

NO
NO
YES
YES
YES

题意

从头到后遍历,当前字母如果和前面的字母一样则不算,如果当前的字母和前一个的字母不一样且在前面出现过输出NO,没有则输出YES

思路

  • 1.连续的相同的字母不算
  • 2.当前的字母在前面出现过输出NO
  • 3.所有字母都是连续的相同的不同字母输出YES

坑点

  • 1.第一个字母
  • 2.数组未初始化
  • 3.特判

代码

// #include<bits/stdc++.h>
// using namespace std;
// int haxi[300];
// int main()
// {
//      int n;
//      cin>>n;
//      while(n--)
//      {
//      	int m,res=0;
//      	memset(haxi,0,sizeof(haxi));
//      	cin>>m;
//      	getchar();
//      	string s;
//      	cin>>s;
//      	for(int i=0;i<m;i++)
//      	{
//      		haxi[s[i]]++;
//      		if(s[i]!=s[i-1]&&haxi[s[i]]>=2&&res==0)
//      		{
//      			res=1;
//      			cout<<"NO"<<endl;
//      			break;
// 			}
// 		}
// 		if(res==0) cout<<"YES"<<endl;
// 	 }
// 	return 0;
// }

#include<bits/stdc++.h>
using namespace std;
int haxi[300];//利用哈希看是否出现过
int main()
{
	ios::sync_with_stdio(false);//快读
	cin.tie(0),cout.tie(0);
	int t;
	cin>>t;
	while(t--)
	{
		memset(haxi,0,sizeof(haxi));//初始化数组
		int x;
		cin>>x;
		string s;
		cin>>s;
		haxi[s[0]]++;//第一个字母
		int res=0;
		for(int i=1;i<x;i++)
		{
			if(i==1)
			{
				if(s[i]==s[i-1])
				{
					continue;
				}
			}
			if(i!=1)
			{
				if(s[i]==s[i-1])
				{
					continue;
				}else{
					if(haxi[s[i]]>0)
					{
						cout<<"NO"<<endl;
						res++;
						break;
					}
				}
			}
			haxi[s[i]]++;
		}
		if(res==0)
		{
			cout<<"YES"<<endl;
		}
	}
	return 0;
}

总结

注意细节问题,容易发生小错误导致bug,调试过久

B. Ordinary Numbers

Let’s call a positive integer n ordinary if in the decimal notation all its digits are the same. For example, 1, 2 and 99 are ordinary numbers, but 719 and 2021 are not ordinary numbers.

输入格式

The first line contains one integer t (1≤t≤104). Then t test cases follow.

Each test case is characterized by one integer n (1≤n≤109).

输出格式

For each test case output the number of ordinary numbers among numbers from 1 to n.

样例输入

6
1
2
3
4
5
100

样例输出

1
2
3
4
5
18

题意

求1-n中有多少个数位上数字都相同的数(如:1,11,222,3333)

思路

  • 1.找规律,1位数(1-9)有9个这样的数,2位数(10-99)有9个,x位数依次递增
  • 2.可知,x位数有x*9个这样的数
  • 3.在此加上pow(10,x)-n中有几个相同的数

坑点

  • 1.暴力超时

代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    //ios::sync_with_stdio(false);
    //cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        int n=0;
        int sum=0;
        cin>>n;
        int w=n;
        int cnt=0;
        while(n>9){
            cnt++;//求出几位数
            n=n/10;
        }
        int x=1;
        sum=cnt*9;//计算cnt*9
        for(int i=0;i<cnt;i++)
        {
            x=x*10;
        }
        int res=1;
        int q=1;
        while(1)
        {
            res=q;
            for(int i=0;i<cnt;i++)
            {
                res=res*10+q;
            }
            q++;
            if(res>w)break;
            sum++;
        }
        cout<<sum<<endl;
    }
    return 0;
}

总结

小思维题

C. Not Adjacent Matrix

We will consider the numbers a and b as adjacent if they differ by exactly one, that is, |a−b|=1.

We will consider cells of a square matrix n×n as adjacent if they have a common side, that is, for cell (r,c) cells (r,c−1), (r,c+1), (r−1,c) and (r+1,c) are adjacent to it.

For a given number n, construct a square matrix n×n such that:

Each integer from 1 to n2 occurs in this matrix exactly once;
If (r1,c1) and (r2,c2) are adjacent cells, then the numbers written in them must not be adjacent.

输入格式

The first line contains one integer t (1≤t≤100). Then t test cases follow.

Each test case is characterized by one integer n (1≤n≤100).

输出格式

For each test case, output:

-1, if the required matrix does not exist;
the required matrix, otherwise (any such matrix if many of them exist).
The matrix should be outputted as n lines, where each line contains n integers.

样例输入

3
1
2
3

样例输出

1
-1
2 9 7
4 6 3
1 8 5

题意

填数游戏,nn个方格中填数(1-nn),每填一个数x,他的上下左右相邻的方格中不能填x+1或x-1

思路

  • 1.两重循环
  • 2.每一行依次填,填一个标记相邻的空格,把能填的空格填完后
  • 3.将标记过的空格依次赋值剩下的数字。

坑点

  • 1.不需要用搜索或者递归
  • 2.数组初始化
  • 3.只有n等于2时,输出-1

代码

#include<bits/stdc++.h>
using namespace std;
int ans[105][105];
int res[105][105];
int w;
int cnt=0;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
    	memset(ans,0,sizeof(ans));
    	memset(res,0,sizeof(res));
        cin>>w;
        ans[1][1]=1;
        res[1][1]=1;
		res[1+1][1]=1;
		res[1][1+1]=1;
		res[1-1][1]=1;
		res[1][1-1]=1;
		int u=2;
        for(int i=1;i<=w;i++)
        {
        	for(int j=1;j<=w;j++)
	        {
	        	if(res[i][j]==0)
			    {
			        ans[i][j]=u;
			        u++;
			        res[i][j]=1;
			        res[i+1][j]=1;
			        res[i][j+1]=1;
			        res[i-1][j]=1;
			        res[i][j-1]=1;
			    }
			}
		}
		    for(int i=1;i<=w;i++)
        {
            for(int j=1;j<=w;j++)
            {
                if(ans[i][j]==0)
                {
                    ans[i][j]=-1;
                }
            }
        }
        if(w==2)
        {
        	cout<<-1<<endl;
        	continue;
		}
        int e=1;
        if(w%2==0)
        {
        	cnt=w*w/2+1;
		}else{
			cnt=w*w/2+2;
		}
        while(1)
        {
            int q=0;
            for(int j=1;j<=w;j++)
            {
                for(int k=1;k<=w;k++)
                {
                    if(ans[j][k]==-1)
                    {
                        ans[j][k]=cnt;
                        cnt++;
                        q++;
                        break;
                    }
                }
                if(q>=1)
                {
                	break;
				}
            }
            if(cnt>w*w)break;
        }
        for(int j=1;j<=w;j++)
            {
                for(int k=1;k<=w;k++)
                {
                    cout<<ans[j][k]<<" ";
                }
                cout<<endl;
            }
    }
    return 0;
}

总结

模拟题,确认策略后模拟即可,递归超时

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霖承科技 LinChance

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值