STl的一些具体的例题的应用
来源:互联网 发布:java编程思想 新手 编辑:程序博客网 时间:2024/06/05 14:32
HDU 1004
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 115929 Accepted Submission(s): 45434
This year, they decide to leave this lovely job to you.
A test case with N = 0 terminates the input and this test case is not to be processed.
5greenredblueredred3pinkorangepink0
redpink
该题需要一一对应,因此用的是map
已经AC过的代码:
#include<cstdio>
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
int n;
string a;
while(cin>>n&&n>0)
{
map<string, int>ss;
while(n--)
{
cin>>a;
ss[a]++;
}
int max=0;
string ans;
map<string, int>::iterator it;
for(it=ss.begin();it!=ss.end();it++)
{
if(it->second>max)
{
max=(*it).second;
ans=(*it).first;
}
}
cout<<ans<<endl;
}
return 0;
}
sdnuoj
1174.明明的随机数
Total Submission(s): 138 Accepted Submission(s): 56
Description
Input
Output
Sample Input
1020 40 32 67 40 20 89 300 400 15
Sample Output
815 20 32 40 67 89 300 400
set对于已经出现的数不会再重新记录,而且set对于数据具有自动排序的功能
已经AC过的代码:
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
int main()
{
set<int>s;
s.clear();
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
int m;
scanf("%d",&m);
s.insert(m);
}
int sum;
sum=s.size();
printf("%d\n",sum);
set<int >::iterator it1,it2;
it1=--s.end();
for(it2=s.begin();it2!=--s.end();it2++)
printf("%d ",*it2);
printf("%d\n",*it1);
return 0;
}
HDU1276
士兵队列训练问题
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7702 Accepted Submission(s): 3524
22040
1 7 191 19 37
已经AC过的代码:
方法一:用普通的数组:
#include<cstdio>
#include<cstring>
int main()
{
int t,n,k,i;
int a[5005],b[5005];
while(scanf("%d",&t)!=EOF)
{
while(t--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++)
a[i]=i;
while(1)
{
if(n<=3)
{
for( i=1; i<n; i++)
printf("%d ",a[i]);
printf("%d\n",a[i]);
break;
}
k=0;
for(int i=1; i<=n; i++)
{
if(i%2!=0)
b[++k]=a[i];
}
if(k<=3)
{
for(int i=1; i<k; i++)
printf("%d ",b[i]);
printf("%d\n",b[k]);
break;
}
n=0;
for(int i=1; i<=k; i++)
{
if(i%3!=0)
a[++n]=b[i];
}
}
}
}
return 0;
}
方法二:用队列完成的:
#include <iostream>
#include <queue>
using namespace std;
queue <int> s;
void cmp(int k)
{
int i = 1;
while(s.front() != 0)
{
if(i % k != 0) //用取模来实现数数的循环来数到相应的数字
s.push(s.front()); //把留下来的方队尾
s.pop(); //去掉队首
++i;
}
s.pop(); //把队首的0去掉,在队尾加上0
s.push(0);
}
int main()
{
int t, n;
cin >> t;
while(t-- && cin >>n)
{
for(int i = 1; i <=n; i++)
s.push(i);
s.push(0); //在最后面加个0代表队尾的标志
int i = 1;
while(s.size() > 4) //因为加上了0,所以长度小于等于4就退出
{
if(i % 2 == 0) //选择数三下踢一个人还是数两下踢一个人
cmp(3); //i是偶数数则cmp(3),否则cmp(2)
else
cmp(2);
i++;
}
while(!s.empty())
{
if(s.front() != 0)
{
cout << s.front();
s.pop();
if(s.front() != 0)
cout << " ";
}
else
s.pop();//因为队尾还有个0,这个是不用输出的但是为了清空队列,还是要pop他
}
cout << endl;
}
return 0;
}
方法三:使用链表的方式:
#include<iostream>
#include<list>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
list<int> s;
if(n==0) cout<<0<<endl;
else
{
for(int i=1; i<=n; i++)
s.push_back(i);
list<int>::iterator it,ip;//链表遍历迭代器
int f=1;
int flag=s.size();
while(flag>3)
{
for(it=s.begin(); it!=s.end();)
{
if(f==2)
{
f=1;
ip=it;
it++;
s.erase(ip); //抹除操作
flag--;
}
else
{
f++;
it++;
}
}
f=1;
if(flag<=3) break;
for(it=s.begin(); it!=s.end();)
{
if(f==3)
{
f=1;
ip=it;
it++;
s.erase(ip);
flag--;
}
else
{
f++;
it++;
}
}
f=1;
if(flag<=3) break;
}
int w=0;
for(it=s.begin(); it!=s.end(); it++)
{
cout<<(*it); //注意输出格式
w++;
if(w<=flag-1) cout<<' ';
}
cout<<endl;
}
}
return 0;
}
HDU 5831
Rikka with Parenthesis II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1408 Accepted Submission(s): 631
Correct parentheses sequences can be defined recursively as follows:
1.The empty string "" is a correct sequence.
2.If "X" and "Y" are correct sequences, then "XY" (the concatenation of X and Y) is a correct sequence.
3.If "X" is a correct sequence, then "(X)" is a correct sequence.
Each correct parentheses sequence can be derived using the above rules.
Examples of correct parentheses sequences include "", "()", "()()()", "(()())", and "(((())))".
Now Yuta has a parentheses sequence S, and he wants Rikka to choose two different positioni,j and swap S_i,S_j.
Rikka likes correct parentheses sequence. So she wants to know if she can change S to a correct parentheses sequence after this operation.
It is too difficult for Rikka. Can you help her?
For each testcase, the first line contains an integers n(1<=n<=100000), the length of S. And the second line contains a string of length S which only contains ‘(’ and ‘)’.
34())(4()()6)))(((
YesYesNo
这道题使用stack的方法更方便一点
已经AC过的代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
using namespace std;
int main()
{
int T,n;
char a[100010];
stack<char>s;
scanf("%d",&T);
while(T--)
{
int l=0,r=0,ans=0,wa=0,flag=1;
while(!s.empty())
s.pop();
scanf("%d",&n);
if(n)
scanf("%s",a);
if(n%2==1) printf("No\n");
else
{
int len=strlen(a);
for(int i=0; i<len; i++)
{
if(a[i]=='(')
{
l++;
s.push(a[i]);
}
else
{
r++;
if(!s.empty()&&s.top()=='(')
{
ans++;
s.pop();
}
else
{
wa++;
}
}
}
if(l==r)
{
if(s.empty())
{
if(ans!=1)printf("Yes\n");
else printf("No\n");
}
else if(wa==1)
printf("Yes\n");
else if(wa==2&&!s.empty())
printf("Yes\n");
else
printf("No\n");
}
else printf("No\n");
}
}
return 0;
}
- STl的一些具体的例题的应用
- stl的经典例题
- [ZT]一些iptables的具体应用
- 一些例题的技巧
- 并查集的具体例题
- sql server 2005数据库的一些应用例题
- log4j的具体应用
- SD2303的具体应用
- 二分法的具体应用
- c语言的一些例题
- 后缀数组的例题应用
- 例题5-3 UVa10815 Andy's First Dictionary(STL:set&&sstream的应用)
- 并查集的具体例题(1218 认路)
- 具体一些的博弈论 sqrstone
- STL容器中元素的具体需求
- 流媒体协议的具体应用
- 命令模式的具体应用
- 后缀树的具体应用
- mysql date_add函数
- 利用 LeakCanary 来检查 Android 内存泄漏
- Java并发编程(3)-- Thread
- redis StackExchange 主备 实现 demo
- Spring+SpringMVC+MyBatis+easyUI整合基础篇(四)代码简化
- STl的一些具体的例题的应用
- sql语句获取想要字段中值与实际值不一样时的拼写
- 安卓SQLite打开数据库的错误
- Linux UDP套接字编程
- 【LeetCode】1. Two Sum
- imx6q led灯驱动及测试代码ioctl(自动创建设备文件v2)
- Quartz
- 02JVM-内存溢出
- Objective-C类的静态数据成员