几个南阳oj的STL题

来源:互联网 发布:教师网络信息管理平台 编辑:程序博客网 时间:2024/05/16 09:18

D的小L

时间限制:4000 ms  |  内存限制:65535 KB
难度:2
描述
      一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给匡匡出了个题目想难倒匡匡(小L很D吧),有一个数n(0<n<10),写出1到n的全排列,这时匡匡有点囧了,,,聪明的你能帮匡匡解围吗?
输入
第一行输入一个数N(0<N<10),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个整数x(0<x<10)
输出
按特定顺序输出所有组合。
特定顺序:每一个组合中的值从小到大排列,组合之间按字典序排列。
样例输入
223
样例输出
1221123132213231312321
来源
原创
上传者
kapop



代码:

 #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

时间限制:300 ms  |  内存限制:65535 KB
难度:3
描述

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.

输入
The input has multicases and each case contains a integer N.
输出
For each case,output the smallest integer greater than N that has the same binary weight as N.
样例输入
17174712555555
样例输出
171881117555557
来源
topcoder
上传者
骆魁永


代码:

 #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;}        




求次数

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述

题意很简单,给一个数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
样例输入
22aaaaaaa3acmacm
样例输出
51
上传者
ACM_王亚龙


代码:


 #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++;elsem[ch]=1;}printf("%d\n",ans);}return 0;}        



Registration system

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述

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.
样例输入
4abacabaacabaabacabaacab
样例输出
OKOKabacaba1OK
来源
爱生活
上传者
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;}        



汉诺塔(三)

时间限制:3000 ms  |  内存限制:65535 KB
难度:3
描述

在印度,有这么一个古老的传说:在世界中心贝拿勒斯(在印度北部)的圣庙里,一块黄铜板上插着三根宝石针。印度教的主神梵天在创造世界的时候,在其中一根针上从下到上地穿好了由大到小的64片金片,这就是所谓的汉诺塔。不论白天黑夜,总有一个僧侣在按照下面的法则移动这些金片:一次只移动一片,不管在哪根针上,小片必须在大片上面。僧侣们预言,当所有的金片都从梵天穿好的那根针上移到另外一根针上时,世界就将在一声霹雳中消灭,而梵塔、庙宇和众生也都将同归于尽。


现在我们把三根针编号为1,2,3。

所有的金片在初始时都在1号针上,现在给你的任务是判断一系列的指令过程中,是否会出现非法的指令。

而非法指令有以下两种情况:

1、某个针上已经没有金片了,但是指令依然要求从该处移动金片到其它针上。

2、把一个大的金片移动到了小的金片上。

输入
第一行输入一个整数N表示测试数据的组数(N<10)
每组测试数据的第一行有两个整数P,Q(1<P<64,1<Q<100),分别表示汉诺塔的层数与随后指令的条数
随后的Q行,每行都输入两个整数a,b,(1<=a,b<=3)表示一条指令。
指令1 2表示把1号针最上面的金片移动到2号针最上面。
数据保证a,b不会相同。
输出
如果存在非法指令,请输出illegal
不存在非法指令则输出legal
样例输入
32 11 23 31 21 33 22 12 1
样例输出
legalillegalillegal
来源
[张云聪]原创
上传者
张云聪

代码:


 #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");elseprintf("illegal\n");}return 0;}        






0 1
原创粉丝点击