D的小L
-
描述
- 一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给匡匡出了个题目想难倒匡匡(小L很D吧 ),有一个数n(0<n<10),写出1到n的全排列,这时匡匡有点囧了 ,,,聪明的你能帮匡匡解围吗?
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int shu[10];
int main(){
int t;scanf("%d",&t);
while (t--)
{
int n;scanf("%d",&n);
for (int i=0;i<n;i++)
shu[i]=i+1;
do {
for (int i=0;i<n;i++)
printf("%d",shu[i]);
printf("\n");
}while (next_permutation(shu,shu+n));
}
return 0;
}
Same binary weight
-
描述
-
The binary weight of a positive integer is the number of 1's in its binary representation.for example,the decmial number 1 has a binary weight of 1,and the decimal number 1717 (which is 11010110101 in binary) has a binary weight of 7.Give a positive integer N,return the smallest integer greater than N that has the same binary weight as N.N will be between 1 and 1000000000,inclusive,the result is guaranteed to fit in a signed 32-bit interget.
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int shu[35],ll,huo[35];
int main()
{
int n;
while (~scanf("%d",&n))
{
memset(huo,0,sizeof(huo));
memset(shu,0,sizeof(shu));
ll=0;
while (n)
{
shu[ll++]=n%2;
n/=2;
}
for (int i=0;i<32;i++)
huo[i]=shu[31-i];
next_permutation(huo,huo+32);
for (int i=0;i<32;i++)
n=n*2+huo[i];
printf("%d\n",n);
}
return 0;
}
求次数
-
描述
-
题意很简单,给一个数n 以及一个字符串str,区间【i,i+n-1】 为一个新的字符串,i 属于【0,strlen(str)】如果新的字符串出现过ans++,例如:acmacm n=3,那么 子串为acm cma mac acm ,只有acm出现过
求ans;
-
输入
-
LINE 1: T组数据(T<10)
LINE 2: n ,n <= 10,且小于strlen(str);
LINE 3:str
str 仅包含英文小写字母 ,切长度小于10w
输出
- 求 ans 样例输入
-
2 2 aaaaaaa 3 acmacm
样例输出
-
5 1
上传者
- ACM_王亚龙
-
LINE 1: T组数据(T<10)
代码:
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int n;
char str[100100],ch[15];
void lstrcpy(char xx[],char yy[],int i,int n)
{
for (int j=0;j<n;j++)
xx[j]=yy[i+j];
xx[n]=0;
return;
}
int main()
{
int t;scanf("%d",&t);
while (t--)
{
scanf("%d%s",&n,str);
map<string ,int> m;
int ll=strlen(str);
int ans=0;
for (int i=0;i<=ll-n;i++)
{
lstrcpy(ch,str,i,n);
if (m[ch])
ans++;
else
m[ch]=1;
}
printf("%d\n",ans);
}
return 0;
}
Registration system
-
描述
-
A new e-mail service "Berlandesk" is going to be opened in Berland in the near future.
The site administration wants to launch their project as soon as possible, that's why they
ask you to help. You're suggested to implement the prototype of site registration system.
The system should work on the following principle.
Each time a new user wants to register, he sends to the system a request with his name.
If such a name does not exist in the system database, it is inserted into the database, and
the user gets the response OK, confirming the successful registration. If the name already
exists in the system database, the system makes up a new user name, sends it to the user
as a prompt and also inserts the prompt into the database. The new name is formed by the
following rule. Numbers, starting with 1, are appended one after another to name (name1,
name2, ...), among these numbers the least i is found so that namei does not yet exist in
the database.
-
输入
- The first line contains number n (1 ≤ n ≤ 105). The following n lines contain the requests to the system. Each request is a non-empty line, and consists of not more than 1000 characters, which are all lowercase Latin letters. 输出
- Print n lines, which are system responses to the requests: OK in case of successful registration, or a prompt with a new name, if the requested name is already taken. 样例输入
-
4 abacaba acaba abacaba acab
样例输出
-
OK OK abacaba1 OK
来源
- 爱生活 上传者
- TCM_张鹏
代码:
#include<cstdio>
#include<map>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int n;
char ch[1005];
int main()
{
int n;scanf("%d",&n);
map<string ,int> m;
for (int i=0;i<n;i++)
{
scanf("%s",ch);
if (m[ch])
{
printf("%s%d\n",ch,m[ch]);
m[ch]++;
}
else
{
m[ch]=1;
printf("OK\n");
}
}
return 0;
}
汉诺塔(三)
-
描述
-
在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针。印度教的主神梵天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片,这就是所谓的汉诺塔。不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金片:一次只移动一片,不管在哪根针上,小片必须在大片上面。僧侣们预言,当所有的金片都从梵天穿好的那根针上移到另外一根针上时,世界就将在一声霹雳中消灭,而梵塔、庙宇和众生也都将同归于尽。
现在我们把三根针编号为1,2,3。
所有的金片在初始时都在1号针上,现在给你的任务是判断一系列的指令过程中,是否会出现非法的指令。
而非法指令有以下两种情况:
1、某个针上已经没有金片了,但是指令依然要求从该处移动金片到其它针上。
2、把一个大的金片移动到了小的金片上。
代码:
#include<cstdio>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
int t;scanf("%d",&t);
while (t--)
{
int h,q,a,b;
bool fafe=true;
scanf("%d%d",&h,&q);
stack<int>sta[4];
for (int i=0;i<h;i++)
sta[1].push(h-i);
for (int i=0;i<q;i++)
{
scanf("%d%d",&a,&b);
if (fafe)
{
if (sta[a].empty())
{
fafe=false;
continue;
}
int aa=sta[a].top();
sta[a].pop();
if (sta[b].empty())
{
sta[b].push(aa);
continue;
}
int bb=sta[b].top();
if (aa>bb)
{
fafe=false;
continue;
}
sta[b].push(aa);
}
}
if (fafe)
printf("legal\n");
else
printf("illegal\n");
}
return 0;
}