codeforces-745【思维】

来源:互联网 发布:兄弟连高洛峰 php教程 编辑:程序博客网 时间:2024/05/16 12:56

题目链接:点击打开链接

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 3XXXXXX
output
YES
input
2 2.XXX
output
NO
input
5 5.......X.................
output
YES
Note

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

111222111222

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");elseputs("NO");}return 0;}



0 0