题目链接:点击打开链接
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.
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 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.
abcd
4
bbb
1
yzyz
2
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;
}*/
题目链接: 点击打开链接
#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;
}