STL编程
#1泛型函数的调用
可以简便的输入数据类型
代码片段
template < typename T >
T max1(T x,T y) {
return x > y ? x : y;
}
#2 < vector > 容器
vector容器能存放任何类型的对象
#3访问方式
下标方式访问元素
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v(3);
v[0]=2;
v[1]=7;
v[2]=9;
cout<<v[0]<<" "<<v[1]<<" "<<v[2]<<endl;
return 0;
}
用迭代器访问元素
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> v(3);
v[0]=2;
v[1]=7;
v[2]=9;
vector<int>::iterator it;
for(it=v.begin();it!=v.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
return 0;
}
元素插入与删除
v.insert(v.begin,8) 在头部插入
v.insert(v.begin()+2,1); 在下标2处插入
v.insert(v.end(),3); 末尾插入
v.erase(v.begin()+2) 删除第3个元素
v.erase(v.begin()+1,v.begin()+5); 删除
下标1-下标4的元素
使用reverse反向排列算法
#include<iostream>
#include<vector>
#include<algorithm> //因为算法,所以#include<algorith>不可少
using namespace std;
int main()
{
vector<int> v(10);
for(int i=0;i<10;i++)
{
v[i]=i;
}
reverse(v.begin(),v.end());
vector<int>::iterator it;
for(it=v.begin();it!=v.end();it++)
{
cout<<*it<<" ";
}
cout<<endl;
return 0;
}
Sort
它的排序范围是[first,last),包括first,
不包括last
(一)例题记录
Text Reverse
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
Input
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
Output
For each test case, you should output the text which is processed.
Smaple Input
3
olleh !dlrow
m’I morf .udh
I ekil .mca
Sample Output
hello world!
I’m from hdu.
I like acm.
AC代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
char c;
scanf("%d",&n);
getchar();
while(n--)
{
stack<char> s;
while(true)
{
c = getchar();
if(c==' '|| c == '\n' || c == EOF)
{
while(!s.empty())
{
printf("%c",s.top());
s.pop();
}
if(c=='\n' || c==EOF) break;
printf(" ");
}
else s.push(c);
}
printf("\n");
}
return 0;
}
排列
Ray又对数字的列产生了兴趣:
现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数.
Input
每组数据占一行,代表四张卡片上的数字(0<=数字<=9),如果四张卡片都是0,则输入结束。
Output
对每组卡片按从小到大的顺序输出所有能由这四张卡片组成的4位数,千位数字相同的在同一行,同一行中每个四位数间用空格分隔。
每组输出数据间空一行,最后一组数据后面没有空行
Sample Input
1 2 3 4
1 1 2 3
0 1 2 3
0 0 0 0
Sample Output
1234 1243 1324 1342 1423 1432
2134 2143 2314 2341 2413 2431
3124 3142 3214 3241 3412 3421
4123 4132 4213 4231 4312 4321
1123 1132 1213 1231 1312 1321
2113 2131 2311
3112 3121 3211
1023 1032 1203 1230 1302 1320
2013 2031 2103 2130 2301 2310
3012 3021 3102 3120 3201 3210
AC代码
#include <bits/stdc++.h>
using namespace std;
int a[4];
int main()
{
bool flag = false;
while(scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3]) != EOF){
if(a[0] == 0 && a[1] ==0 && a[2] ==0 && a[3] ==0 )
break;
if(flag)
printf("\n");
flag = true;
int lasta0 = -1;
bool flag2 = true;
do{
if(a[0] == 0)
continue;
if(flag2)
flag2 = false;
else if(lasta0==a[0])
printf(" ");
else
printf("\n");
printf("%d%d%d%d",a[0],a[1],a[2],a[3]);
lasta0=a[0];
}while(next_permutation(a,a+4));
printf("\n");
}
return 0;
}
复合词
You are to find all the two-word compound words in a dictionary. A two-word compound word is a word in the dictionary that is the concatenation of exactly two other words in the dictionary
Input
Standard input consists of a number of lowercase words, one per line, in alphabetical order. There will be no more than 120,000 words.
Output
Your output should contain all the compound words, one per line, in alphabetical order.
Sample Input
a
alien
born
less
lien
never
nevertheless
new
newborn t
he
zebra
Sample Out
alien
newborn
AC 代码
#include<bits/stdc++.h>
using namespace std;
set<string>c;
int main()
{
string s;
set<string> :: iterator it;
while(cin >> s) c.insert(s);
for(it = c.begin(); it != c.end();it++){
s = *it;
for(int i = 0;i < s.length() - 1;i++)
{
string s1 = s.substr(0,i+1);
string s2 = s.substr(i+1,s.length()-1);
if(c.find(s1) != c.end() && c.find(s2) != c.end())
{
cout << s << endl;
break;
}
}
}
return 0;
}