清明节给大一的孩子们讲了点STL的应用,下了些功夫,搬到这里来供大家学习交流。
1.泛型程序设计简介与迭代器的介绍
2.常见的STL容器及其例题应用(UVA10474,UVA101,UVA10815,UVA156,UVA540,UVA136
HDU1027,CF501B,HDU1716,HDU4277)
3.相关练习和思路
1.泛型程序设计简介与迭代器的介绍
1.1 泛型程序设计简介
泛型程序设计,简单地说就是使用模板的程序设计法。将一些常用的数据结构(比如链表,数组,二叉树)和算法(比如排序,查找)写成模板,以后则不论数据结构里放的是什么对象,算法针对什么样的对象,则都不必重新实现数据结构,重新编写算法。
总而言之,不多赘述,有了STL,不必再从头写大多的标准数据结构和算法,并且可获得非常高的性能。
1.2迭代器
| 操作 | 效果 |
| begin() | 返回一个迭代器,指向第一个元素 |
| end() | 返回一个迭代器,指向最后一个元素之后 |
/*********************************
日期:2015-04-05
作者:matrix68
题号: UVA 10474 - Where is the Marble?
总结:sort使用练习
Tricks:
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
const double PI = acos(-0);
const int MAXN = 10000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
using namespace std;
int arr[MAXN];
int hash[MAXN];
int main()
{
// freopen("in.txt","r",stdin);
int n,q,ca=1;
while(scanf("%d %d",&n,&q)&&(n+q))
{
mem(hash,-1);
bool flag=false;
printf("CASE# %d:\n",ca++);
Rep(i,n)
scanf("%d",&arr[i]);
sort(arr,arr+n);
Rep(i,n)
{
if(arr[i]!=arr[i-1])//不加的话会wa
hash[arr[i]]=i+1;
}
int ques;
Rep(i,q)
{
flag=false;
scanf("%d",&ques);
if(hash[ques]!=-1)
printf("%d found at %d\n",ques,hash[ques]);
else
printf("%d not found\n",ques);
}
}
return 0;
}
/*********************************
日期:2015-04-05
作者:matrix68
题号: UVA 10815 - Andy's First Dictionary
总结:set使用练习
Tricks:
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
const double PI = acos(-0);
const int MAXN = 1000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
using namespace std;
set<string>jihe;
int main()
{
// freopen("in.txt","r",stdin);
string sen;
while(getline(cin,sen))
{
for(int i=0;i<sen.size();i++)
{
if(!isalpha(sen[i])) continue;
string tmp;
while(isalpha(sen[i]))
{
tmp+=tolower(sen[i]);
i++;
}
jihe.insert(tmp);
}
}
for(set<string>::iterator it=jihe.begin();it!=jihe.end();it++)
{
cout<<*it<<endl;
}
return 0;
}<strong>
</strong>/*********************************
日期:2015-04-05
作者:matrix68
题号: UVA 156 - Ananagrams
总结:map+set+vector使用练习
Tricks:
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
const double PI = acos(-0);
const int MAXN = 1000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
using namespace std;
map<string,int> hash;//标准句子出现次数
string record[MAXN];//所有原来的句子
set<string> ans;//所有答案
void standard(string& s1)
{
string ans;
Rep(i,s1.size())
s1[i]=tolower(s1[i]);
sort(s1.begin(),s1.end());
}
int main()
{
// freopen("in.txt","r",stdin);
string sen;
int index=0;
while(getline(cin,sen))
{
if(sen[0]=='#')
break;
Rep(i,sen.size())
{
string tmp;
if(!isalpha(sen[i]))continue;
while(isalpha(sen[i]))
{
tmp+=sen[i];
i++;
}
record[index++]=tmp;
standard(tmp);
hash[tmp]++;
}
}
Rep(i,index)
{
string tmp=record[i];
standard(tmp);
if(hash[tmp]==1)
ans.insert(record[i]);
}
for(set<string>::iterator it=ans.begin(); it!=ans.end(); it++)
cout<<*it<<endl;
return 0;
}
/*********************************
日期:2015-04-05
作者:matrix68
题号: UVA 156 - Ananagrams
总结:multimap使用练习
Tricks:string利用构造函数拷贝
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define SZ(x) (int)x.size()
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
const double PI = acos(-0);
const int MAXN = 1000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
using namespace std;
multimap<string,string> hash;//从标准到原句子
set<string> ans;//所有标准句子
void standard(string& s1)
{
string ans;
Rep(i,s1.size())
{
s1[i]=tolower(s1[i]);
}
sort(s1.begin(),s1.end());
}
int main()
{
freopen("in.txt","r",stdin);
string sen;
while(getline(cin,sen))
{
if(sen[0]=='#')
break;
Rep(i,sen.size())
{
string tmp;
if(!isalpha(sen[i]))continue;
while(isalpha(sen[i]))
{
tmp+=sen[i];
i++;
}
string ttmp=tmp;
standard(tmp);
hash.insert(MP(tmp,ttmp));
}
}
for(map<string,string>::iterator it=hash.begin();it!=hash.end(); it++)
{
// cout<<it->second<<" "<<hash.count(it->second)<<endl;
if(hash.count(it->first)==1)
ans.insert(it->second);
}
for(set<string>::iterator it=ans.begin(); it!=ans.end(); it++)
{
cout<<*it<<endl;
}
return 0;
}
/*********************************
日期:2015-04-05
作者:matrix68
题号: UVA 540 - Team Queue
总结:queue使用练习
Tricks:可能重复入队出队
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define SZ(x) (int)x.size()
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
const double PI = acos(-0);
const int MAXN = 1000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
using namespace std;
int main()
{
// freopen("in.txt","r",stdin);
int T;
int ca=1;
while(scanf("%d",&T)&&T)
{
printf("Scenario #%d\n",ca++);
queue<int>zhudui;
queue<int>meige[MAXN];
vector<int>record[MAXN];
bool zhuvis[MAXN];
mem(zhuvis,0);
Rep(j,T)
{
int n;
scanf("%d",&n);
Rep(i,n)
{
int tmp;
scanf("%d",&tmp);
record[j].PB(tmp);
}
}
string s1;
int num;
while(cin>>s1)
{
if(s1=="STOP")
break;
if(s1=="ENQUEUE")
{
cin>>num;
int index=-1;
Rep(i,T)
{
vector<int>::iterator result=find(record[i].begin(),record[i].end(),num);
if(result==record[i].end()) //没找到
continue;
else
{
index=i;
break;
}
}
if(index!=-1)
{
meige[index].push(num);//某人入某队
if(!zhuvis[index])
{
zhuvis[index]=true;
zhudui.push(index);//该队入主队
}
}
}
else if(s1=="DEQUEUE")
{
int duii=zhudui.front();
int ansi=meige[duii].front();
meige[duii].pop();
if(!meige[duii].size())
{
zhudui.pop();
zhuvis[duii]=false;//没有的话会RE
}
cout<<ansi<<endl;
}
}
cout<<endl;
}
return 0;
}
/*********************************
日期:2015-04-05
作者:matrix68
题号: UVA 136 - Ugly Numbers
总结:优先队列使用练习
Tricks:
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define SZ(x) (int)x.size()
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
const double PI = acos(-0);
const int MAXN = 1000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int ref[3]={2,3,5};
using namespace std;
int main()
{
// freopen("in.txt","r",stdin);
priority_queue<LL,vector<LL> ,greater<LL> >pq;
set<LL>record;
pq.push(1);
record.insert(1);
int ca=1;
while(!pq.empty())
{
LL tmp=pq.top();
pq.pop();
if(ca==1500)
{
printf("The 1500'th ugly number is %lld.\n",tmp);
break;
}
Rep(i,3)
{
LL ttmp=tmp*ref[i];
if(!record.count(ttmp))
{
record.insert(ttmp);
pq.push(ttmp);
}
}
ca++;
}
return 0;
}
/*********************************
日期:2015-04-05
作者:matrix68
题号: UVA 136 - Ugly Numbers
总结:set使用练习
Tricks:
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define SZ(x) (int)x.size()
#define viti (vector<int>::iterator)
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define MID(a, b) (a + ((b - a) >> 1)) 1
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
const double PI = acos(-0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
using namespace std;
set<LL>Ch;
int ref[3]={2,3,5};
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int ca=1;
Ch.insert(1);
set<LL>::iterator it=Ch.begin();
while(ca<=2000)
{
it=Ch.begin();
for(it;it!=Ch.end()&&ca<=2000;it++)
{
Rep(j,3)
{
LL tmp=(*it)*ref[j];
if(Ch.find(tmp)==Ch.end())
{
Ch.insert(tmp);
ca++;
}
}
}
}
ca=1;
it=Ch.begin();
for(it;it!=Ch.end()&&ca++<1500;it++);
printf("The 1500'th ugly number is %lld.\n",*it);
return 0;
}
/*********************************
日期:2015-04-05
作者:matrix68
题号: HDU 1027 - Ignatius and the Princess II
总结:next_permutation使用练习
Tricks:
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define SZ(x) (int)x.size()
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
const double PI = acos(-0);
const int MAXN = 1000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int arr[MAXN];
using namespace std;
int main()
{
// freopen("in.txt","r",stdin);
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
arr[i]=i;
int ca=1;
do
{
if(ca==m)
{
int i;
for(i=1;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<arr[i]<<endl;
break;
}
ca++;
}while(next_permutation(arr+1,arr+n+1));
}
return 0;
}/*********************************
日期:2015-04-03
作者:
题号: HDU 1716 - 排列2
总结:next_permutation的使用
Tricks:0开始的忽略
重复的不计
写吐了,while里面乱搞的。。。出题不善,大家跳过这个题吧
**********************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <set>
#include <vector>
#include <map>
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define N 10010
using namespace std;
int vis[12];
int main()
{
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int a[6];
scanf("%d %d %d %d",&a[1],&a[2],&a[3],&a[4]);
if(!(a[1]+a[2]+a[3]+a[4]))
return 0;
do
{
bool mark=false;
mem(vis,0);
for(int i=1; i<=4; i++)
{
vis[a[i]]++;
}
sort(a+1,a+5);
int tmp=a[1];
int cnt=1;
do
{
if(a[1]==0)
continue;
if(a[1]!=tmp)
{
mark=false;
if(tmp!=0)
cout<<endl;
tmp=a[1];
}
else if(vis[a[1]]>1)
{
vis[a[1]]--;
cnt++;
}
if(mark)
{
cout<<" ";
}
else
mark=true;
cout<<a[1]<<a[2]<<a[3]<<a[4];
}while(next_permutation(a+1,a+5));
cout<<endl;
scanf("%d %d %d %d",&a[1],&a[2],&a[3],&a[4]);
if(a[1]+a[2]+a[3]+a[4])
cout<<endl;
else
break;
}while(1);
return 0;
}
/*********************************
日期:2015-04-05
作者:matrix68
题号: Codefores 501B - Misha and Changing Handles
总结:map使用练习
Tricks:建立新名字到旧名字的映射,把所有新名字放在set里排重。
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define SZ(x) (int)x.size()
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
const double PI = acos(-0);
const int MAXN = 1000 + 10;
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
using namespace std;
map<string,string>x_j;//新名字到旧名字的映射
set<string>newn;//新名字
int main()
{
// freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
string s1,s2;
while(T--)
{
cin>>s1>>s2;
if(!(newn.find(s1)==newn.end()))//新名字集合里找到s1
{
x_j[s2]=x_j[s1];
x_j[s1]=x_j[s1];
x_j.erase(s1);
newn.erase(s1);
newn.insert(s2);
}
else
{
newn.insert(s2);
x_j[s2]=s1;
}
}
cout<<newn.size()<<endl;
for(set<string>::iterator it=newn.begin();it!=newn.end();it++)
{
cout<<x_j[*it]<<" "<<*it<<endl;
}
return 0;
}
/*********************************
日期:2015-04-03
作者:matrix68
题号: HDU 4277 - USACO ORZ
总结:DFS+set
Tricks:注意用long long
**********************************/
#include <cstdio>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iomanip>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define SZ(x) (int)x.size()
#define MP(a, b) make_pair(a, b)
#define PB push_back
#define Lowbit(x) ((x) & (-x))
#define Rep(i,n) for(int i=0;i<n;i++)
#define mem(arr,val) memset((arr),(val),(sizeof (arr)))
#define LL long long
#define N 20
#define M 1000000
const double PI = acos(-0);
const int MOD = 1000007;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
using namespace std;
set<LL>jihe;
bool ok(int a,int b,int c)
{
//这里可以防止重复也可以存的时候sort一下
return a+b>c && b+c>a && a+c>b
&& a>=b && b>=c && a!=0 && b!=0 && c!=0;
}
int n;
int side[N];
void DFS(int t,int a,int b,int c)
{
if(t==n)
{
if(ok(a,b,c))
{
LL p1=M*a+b;
jihe.insert(p1);
}
return;
}
a+=side[t];
DFS(t+1,a,b,c);
a-=side[t];
b+=side[t];
DFS(t+1,a,b,c);
b-=side[t];
c+=side[t];
DFS(t+1,a,b,c);
c-=side[t];
}
int main()
{
// freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
jihe.clear();
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&side[i]);
}
DFS(0,0,0,0);
cout<<jihe.size()<<endl;
}
return 0;
}
原文:http://blog.csdn.net/chrome_matrix_68/article/details/44981171