stl粗浅总结
#include<stdio.h>
#include<string.h>
char s[1000005];
char left[1000005];
int main()
{
int i,r=0,l=0,ll=0,change=0;
scanf("%s",s);
for(i=0;i<strlen(s);i++)
{
if(ll<0) break;
switch(s[i])
{
case '{':left[ll]='{';ll++;l++;break;
case '[':left[ll]='[';ll++;l++;break;
case '<':left[ll]='<';ll++;l++;break;
case '(':left[ll]='(';ll++;l++;break;
case '}':if(left[ll-1]!='{') change++;ll--;r++;break;
case ']':if(left[ll-1]!='[') change++;ll--;r++;break;
case ')':if(left[ll-1]!='(') change++;ll--;r++;break;
case '>':if(left[ll-1]!='<') change++;ll--;r++;break;
}
}
if(r!=l||ll!=0) printf("Impossible");
else printf("%d",change);
}
#include<stdio.h>
#include<iostream>
#include<stack>
#include<string.h>
using namespace std;
char s[1000005];
int main()
{
cin>>s;
stack<char> q;
int i,len,ok=1,change=0;
len=strlen(s);
for(i=0;i<len;++i)
{
if(!q.empty())
{
if(s[i]==')'||s[i]=='>'||s[i]=='}'||s[i]==']')
{
switch(q.top())
{
case '{':if(s[i]!='}') ++change;break;
case '[':if(s[i]!=']') ++change;break;
case '<':if(s[i]!='>') ++change;break;
case '(':if(s[i]!=')') ++change;break;
}
q.pop();
}
else q.push(s[i]);
}
else
{
if(s[i]==')'||s[i]=='>'||s[i]=='}'||s[i]==']')
ok=0;
else
{
q.push(s[i]);
}
}
}
if(!q.empty()) ok=0;
if(!ok) printf("Impossible\n");
else cout<<change<<endl;
}
#include<stdio.h>
#include<string.h>
#include<set>
using namespace std;
int u;
int left[300005],right[300005],ll,rr;
set<int> s;
int main()
{
int n,i;
while(~scanf("%d",&n))
{
memset(left,0,sizeof(left));
memset(right,0,sizeof(right));
left[0]=1;ll=1;rr=0;
for(i=0;i<n;i++)
{
scanf("%d",&u);
if(s.count(u))
{
right[rr]=i+1;
left[ll]=i+2;
rr++;ll++;
s.clear();
}
else
{
s.insert(u);
}
}
right[rr-1]=n;
if(rr==0) printf("-1\n");
else
{
printf("%d\n",rr);
for(i=0;i<rr;i++)
{
printf("%d %d\n",left[i],right[i]);
}
}
}
}
接着是第三题 UVA - 10815
10815 - Andy's First Dictionary
Time limit: 3.000 seconds#include<stdio.h>
#include<vector>
#include<string.h>
#include<algorithm>
#include<cctype>
#include<iostream>
using namespace std;
char c,s[10005];
vector<string> q;
int main()
{
int i=0;
while(~(c=getchar()))
{
if(isalpha(c))//判断是否为字母
{
s[i++]=tolower(c);//将大写都转化为小写
}
else if(i!=0)
{
s[i]='\0';
q.push_back(s);
i=0;
}
}
sort(q.begin(),q.end());
for(int u=0;u<q.size();u++)
{
if(u==0||q[u]!=q[u-1])
cout<<q[u]<<endl;
}
}
//注意测试时输入ctrl+z终止输入
Train Problem I
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 42948 Accepted Submission(s): 16077
#include<stdio.h>
#include<string.h>
int main()
{
int num,i,q,p,op[20]={0},o,in[20],out[20],station[20];
char iin[20],oout[20];
while(~scanf("%d %s %s",&num,iin,oout))
{
memset(op,0,sizeof(op));
memset(station,0,sizeof(station));
for(i=0;i<num;i++)
{
in[i]=iin[i]-'0';
out[i]=oout[i]-'0';
}
q=p=0;o=0;
for(i=0;i<num;i++)
{
station[q]=in[i];q++;o++;
while(station[q-1]==out[p])
{
q--;p++;op[o]=1;o++;
}
}
if(p==num)
{
printf("Yes.\n");
for(i=0;i<o;i++)
{
if(op[i]==0) printf("in\n");
else printf("out\n");
}
}
else printf("No.\n");
printf("FINISH\n");
}
}
#include<stdio.h>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
int n;
int oper,element;
int bo[3];
while(~scanf("%d",&n))
{
int Size=0,can=0;
fill(bo,bo+3,1);
stack<int> s;
queue<int> q;
priority_queue<int> p;
for(int i=0;i<n;i++)
{
scanf("%d %d",&oper,&element);
if(oper==1&&can==0)
{
s.push(element);
q.push(element);
p.push(element);
++Size;
}
else if(can==0)
{
if(Size==0) {fill(bo,bo+3,0);can=1;continue;}
if(q.front()!=element) bo[1]=0;
if(s.top()!=element) bo[0]=0;
if(p.top()!=element) bo[2]=0;
s.pop();p.pop();q.pop();--Size;
}
}
int many=0;
for(int i=0;i<3;i++)
{
if(bo[i]==1)many++;
}
if(many==0) printf("impossible\n");
else if(many==1)
{
if(bo[0]==1) printf("stack\n");
if(bo[1]==1) printf("queue\n");
if(bo[2]==1) printf("priority queue\n");
}
else printf("not sure\n");
}
}
第六题 割玻璃(set容器与迭代器的使用)
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm × h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how.
In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments.
After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process.
Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree?
The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000).
Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts.
After each cut print on a single line the area of the maximum available glass fragment in mm2.
4 3 4
H 2
V 2
V 3
V 1
8
4
4
2
7 6 5
H 4
V 3
V 5
H 2
V 1
28
16
12
6
4
#include<stdio.h>
#include<set>
#include<algorithm>
using namespace std;
typedef long long LL;
set<int> w;
set<int> h;
int wi[200005],hi[200005];//此长度的边的数量
void cut(set<int> &q,int x,int *a)
{
set<int>::iterator o,p;
q.insert(x);o=p=q.find(x);
o--;p++;--a[*p-*o];//去掉被破坏的边
++a[x-*o];++a[*p-x];//添加新边
}
int main()
{
int wl,hl,n;
while(~scanf("%d%d%d",&wl,&hl,&n))
{
fill(wi,wi+200005,0);fill(hi,hi+200005,0);
w.clear();h.clear();
w.insert(0);w.insert(wl);
h.insert(0);h.insert(hl);
wi[wl]=1;hi[hl]=1;
while(n--)
{
char s[10];int p;
scanf("%s%d",s,&p);
if(s[0]=='V') cut(w,p,wi);
else cut(h,p,hi);
while(!wi[wl]) --wl;
while(!hi[hl]) --hl;
printf("%lld\n",LL(wl)*LL(hl));
}
}
}
第7题 输出非重复的单词
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<map>
#include<set>
#include<string>
#include<string.h>
using namespace std;
multimap<string,string> q;
set<string> p;
int main()
{
char c,s[25],b[25];
int i=0;
while(cin>>s,s[0]!='#')
{
strcpy(b,s);
int len=strlen(b);
for(int i=0;i<len;i++)
{
if(isalpha(b[i])) b[i]=tolower(b[i]);
}
sort(b,b+len);
q.insert(make_pair(b,s));
}
multimap<string,string>::iterator it;
for(it=q.begin();it!=q.end();it++)
{
if(q.count(it->first)==1)
p.insert(it->second);
}
set<string>::iterator pi;
for(pi=p.begin();pi!=p.end();++pi)
{
cout<<*pi<<endl;
}
return 0;
}
Misha hacked the Codeforces site. Then he decided to let all the users change their handles. A user can now change his handle any number of times. But each new handle must not be equal to any handle that is already used or that was used at some point.
Misha has a list of handle change requests. After completing the requests he wants to understand the relation between the original and the new handles of the users. Help him to do that.
The first line contains integer q (1 ≤ q ≤ 1000), the number of handle change requests.
Next q lines contain the descriptions of the requests, one per line.
Each query consists of two non-empty strings old and new, separated by a space. The strings consist of lowercase and uppercase Latin letters and digits. Strings old and new are distinct. The lengths of the strings do not exceed 20.
The requests are given chronologically. In other words, by the moment of a query there is a single person with handle old, and handle new is not used and has not been used by anyone.
In the first line output the integer n — the number of users that changed their handles at least once.
In the next n lines print the mapping between the old and the new handles of the users. Each of them must contain two strings, old and new, separated by a space, meaning that before the user had handle old, and after all the requests are completed, his handle is new. You may output lines in any order.
Each user who changes the handle must occur exactly once in this description.
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123
#include<stdio.h>
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
char a[25],b[25];
int n;
map<string,string> q;
while(cin>>n)
{
while(n--)
{
cin>>a>>b;
if(q.count(a)!=0)
{
q[b]=q[a];
q.erase(a);
}
else
{
q[b]=a;
}
}
cout<<q.size()<<endl;
map<string,string>::iterator it;
for(it=q.begin();it!=q.end();it++)
{
cout<<it->second<<' '<<it->first<<endl;
}
}
}
值得一提的是map中通过“迭代器->first(second)”的方法表示原元素(映射对象)。Ignatius and the Princess II
Total Submission(s): 9619 Accepted Submission(s): 5629
"Now I will show you the first problem." feng5166 says, "Given a sequence of number 1 to N, we define that 1,2,3...N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it's easy to see the second smallest sequence is 1,2,3...N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It's easy, isn't is? Hahahahaha......"
Can you help Ignatius to solve this problem?
很裸的一道全排列题目只要用到next_permutation(数组头,数组尾)就可以直接进行如题中的排序
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int in[1050];
int m,n;
while(cin>>n>>m)
{
for(int i=0;i<n;i++)
{
in[i]=i+1;
}
for(int i=1;i<m;i++)
{
next_permutation(in,in+n);
}
for(int i=0;i<n-1;i++)
{
cout<<in[i]<<' ';
}
cout<<in[n-1]<<endl;
}
}
总结了一些在stl学习中遇到的题目,因为能力有限只是介绍了一些自己遇到的(主要是容器)的stl问题,希望对读到这篇博客的你有用