codeforces-745【思维】

本文介绍两道编程挑战题目:一是通过循环移位生成不同的字符串并计数;二是利用两块相同拼图尝试拼成一个完整的矩形。文章提供了完整的代码实现及解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

A. Hongcow Learns the Cyclic Shift
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Being a dutiful student, he immediately learns how to spell the word.

Hongcow has decided to try to make new words from this one. He starts by taking the word he just learned how to spell, and moves the last character of the word to the beginning of the word. He calls this a cyclic shift. He can apply cyclic shift many times. For example, consecutively applying cyclic shift operation to the word "abracadabra" Hongcow will get words "aabracadabr", "raabracadab" and so on.

Hongcow is now wondering how many distinct words he can generate by doing the cyclic shift arbitrarily many times. The initial string is also counted.

Input

The first line of input will be a single string s (1 ≤ |s| ≤ 50), the word Hongcow initially learns how to spell. The string s consists only of lowercase English letters ('a'–'z').

Output

Output a single integer equal to the number of distinct strings that Hongcow can obtain by applying the cyclic shift arbitrarily many times to the given string.

Examples
input
abcd
output
4
input
bbb
output
1
input
yzyz
output
2
Note

For the first sample, the strings Hongcow can generate are "abcd", "dabc", "cdab", and "bcda".

For the second sample, no matter how many times Hongcow does the cyclic shift, Hongcow can only generate "bbb".

For the third sample, the two strings Hongcow can generate are "yzyz" and "zyzy".


思路:把字符串按照题意做 len 次循环,然后判断有没有出现过就好了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
using namespace std;
string str;
map<string,bool> M; 
int main()
{
	while(cin>>str)
	{
		M.clear();
		int len=str.length();
//		cout<<str.size()<<endl;
		int ans=0;
		for(int i=1;i<=len;i++)
		{
			if(!M[str])
			{
				ans++;
				M[str]=1;
			}
			str=str[len-1]+str;
			str.erase(str.end()-1,str.end());
		}
		cout<<ans<<endl;
	}
	return 0;
}
/*
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char str[55][55];
int main()
{
	while(~scanf("%s",str[0]))
	{
		int len=strlen(str[0]);
		int ans=1;
		for(int i=1;i<len;i++)
		{
			str[i][0]=str[i-1][len-1];
			for(int j=1;j<len;j++)
			{
				str[i][j]=str[i-1][j-1];
			}
			bool flag=1;
			for(int j=0;j<i;j++)
			{
				if(strcmp(str[i],str[j])==0)
				{
					flag=0;
					break;
				}
			}
			if(flag)
				ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}*/

题目链接: 点击打开链接

B. Hongcow Solves A Puzzle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Hongcow likes solving puzzles.

One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.

The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.

You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position.

Input

The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece.

The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space.

It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region.

Output

Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise.

Examples
input
2 3
XXX
XXX
output
YES
input
2 2
.X
XX
output
NO
input
5 5
.....
..X..
.....
.....
.....
output
YES
Note

For the first sample, one example of a rectangle we can form is as follows

111222
111222

For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.

In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle:

.....
..XX.
.....
.....
.....

大意:给你一块区域,有 ' X ' ,有 ' . ' ,整个区域不能进行旋转或反转,问两个相同的区域拼凑后 ' X ' 是否能构成一个矩形,其中 ' . ' 是空白区域,可以放  ' X ' 

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,m;
char s[510][510];
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		int min_r=510,max_r=0;
		int min_c=510,max_c=0;
		for(int i=1;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				cin>>s[i][j];
				if(s[i][j]=='X')
				{
					min_r=min(i,min_r);
					max_r=max(i,max_r);
					min_c=min(j,min_c);
					max_c=max(j,max_c);
				}
			}
		}
		bool flag=1;
		for(int i=min_r;i<=max_r;i++)
		{
			for(int j=min_c;j<=max_c;j++)
			{
				if(s[i][j]!='X')
				{
					flag=0;
					break;
				}
			}
			if(!flag)
				break;
		}
		if(flag)
			puts("YES");
		else
			puts("NO");
	}
	return 0;
}



### Codeforces 思维题解题思路和技巧 #### 预处理的重要性 对于许多竞赛编程问题而言,预处理能够显著提高效率并简化后续操作。通过提前计算某些固定的数据结构或模式匹配表,可以在实际求解过程中节省大量时间。例如,在字符串处理类题目中预先构建哈希表来加速查找过程[^1]。 #### 算法优化策略 针对特定类型的输入数据设计高效的解决方案至关重要。当面对大规模测试案例时,简单的暴力破解往往无法满足时限要求;此时则需考虑更高级别的算法改进措施,比如动态规划、贪心算法或是图论中的最短路径算法等。此外,合理利用空间换取时间也是一种常见的优化手段[^2]。 #### STL库的应用价值 C++标准模板库提供了丰富的容器类型(vector, deque)、关联式容器(set,map)以及各种迭代器支持,极大地便利了程序开发工作。熟练掌握这些工具不仅有助于快速实现功能模块,还能有效减少代码量从而降低出错几率。特别是在涉及频繁插入删除场景下,优先选用双向队列deque而非单向链表list可获得更好的性能表现。 ```cpp #include <iostream> #include <deque> using namespace std; int main(){ deque<int> dq; // 向两端添加元素 dq.push_back(5); dq.push_front(3); cout << "Front element is: " << dq.front() << endl; cout << "Back element is : " << dq.back() << endl; return 0; } ``` #### 实际应用实例分析 以一道具体题目为例:给定一系列查询指令,分别表示往左端/右端插入数值或者是询问某个指定位置到边界之间的最小距离。此题目的关键在于如何高效地追踪最新状态而无需重复更新整个数组。采用双指针技术配合静态分配的一维数组即可轻松解决上述需求,同时保证O(n)级别的总运行成本[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值