ACM/ICPC竞赛

06篇 ACM/ICPC竞赛之STL--string


字符串是程序中经常要表达和处理的数据,我们通常是采用字符数组或字符指针来表示字符串。STL为我们提供了另一种使用起来更为便捷的字符串的表达方式:stringstring类的定义在头文件<string>中。

string类其实可以看作是一个字符的vectorvector上的各种操作都可以适用于string,另外,string类对象还支持字符串的拼合、转换等操作。

下面先来看一个简单的例子:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     string s = "Hello! ", name;
 9     cin >> name;
10     s += name;
11     s += '!';
12     cout << s << endl;
13     return 0;
14 }

输入:
   string
输出:
  Hello! string!




再以题hduoj 1064--Parencoding为例,看一段用string作为容器,实现由P代码还原括号字符串的示例代码片段:

 1 int m;
 2 cin >> m; // P编码的长度
 3 string str; // 用来存放还原出来的括号字符串
 4 int leftpa = 0; // 记录已出现的左括号的总数
 5 for (int j=0; j<m; j++)
 6 { 
 7     int p;
 8     cin >> p;
 9     for (int k=0; k<p-leftpa; k++)
10          str += '(';
11     str += ')';
12     leftpa = p;
13 }

看下面这个简单的示例:

 1 #include <iostream>
 2 #include <queue>
 3 
 4 using namespace std;
 5 
 6 class T
 7 {
 8     public:
 9 int x, y, z;
10 T(int a, int b, int c):x(a), y(b), z(c)
11 {
12 }
13 };
14 bool operator < (const T &t1, const T &t2)
15 {
16 return t1.z < t2.z; // 按照z的顺序来决定t1和t2的顺序
17 }
18 int main()
19 {
20     priority_queue<T> q;
21     q.push(T(4,4,3));
22     q.push(T(2,2,5));
23     q.push(T(1,5,4));
24     q.push(T(3,3,6));
25 
26     while (!q.empty())
27     {
28         T t = q.top(); q.pop();
29         cout << t.x << " " << t.y << " " << t.z << endl;
30     }
31     return 0;
32 }
33 
34 输出结果为(注意是按照z的顺序从大到小出队的):
35 
36 3 3 6
37 2 2 5
38 1 5 4
39 4 4 3


再看一个按照z的顺序从小到大出队的例子:

 1 #include <iostream>
 2 #include <queue>
 3 
 4 using namespace std;
 5 
 6 class T
 7 {
 8     public:
 9     int x, y, z;
10     T(int a, int b, int c):x(a), y(b), z(c){}
11 };
12 
13 bool operator > (const T &t1, const T &t2)
14 {
15     return t1.z > t2.z;
16 }
17 int main()
18 {
19     priority_queue<T, vector<T>, greater<T> > q;
20     q.push(T(4,4,3));
21     q.push(T(2,2,5));
22     q.push(T(1,5,4));
23     q.push(T(3,3,6));
24 
25     while (!q.empty())
26     {
27         T t = q.top(); q.pop();
28         cout << t.x << " " << t.y << " " << t.z << endl;
29     }
30         return 0;
31 }
32 
33 输出结果为:
34 
35 4 4 3
36 1 5 4
37 2 2 5
38 3 3 6



如果我们把第一个例子中的比较运算符重载为:

1 bool operator < (const T &t1, const T &t2)
2 {
3   return t1.z > t2.z; // 按照z的顺序来决定t1和t2的顺序
4 }



则第一个例子的程序会得到和第二个例子的程序相同的输出结果。

再回顾一下用优先队列实现的题hduoj 1067--Ugly Numbers的代码:

 1 #include <iostream>
 2 #include <queue>
3 using namespace std;
4 typedef pair<unsigned long int, int> node_type;
5 int main() 6 { 7   unsigned long int result[1500]; 8   priority_queue< node_type, vector<node_type>, greater<node_type> > Q; 9   Q.push( make_pair(1, 3) );
10   for (int i=0; i<1500; i++) 11   { 12     node_type node = Q.top(); 13     Q.pop(); 14     switch(node.second) 15     { 16       case 3: Q.push( make_pair(node.first*2, 3) ); 17       case 2: Q.push( make_pair(node.first*3, 2) ); 18       case 1: Q.push( make_pair(node.first*5, 1) ); 19     } 20     result = node.first; 21   } 22   int n; 23   cin >> n; 24   while (n>0) 25   { 26     cout << result[n-1] << endl; 27     cin >> n; 28   } 29   return 0; 30 }

未完待续》》》

 

 

 

原文地址:https://www.cnblogs.com/jeff-wgc/p/4480300.html