STL学习

UVA10815

注意输出流的用法。

 1 #include 
 2 #include <set>
 3 #include 
 4 #include 
 5 #include 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     string s,s1;
11     char c;
12     set<string> ss;
13     while(getline(cin,s)){
14         int len = s.length();
15         for(int i = 0; i < len; i++){
16             if(isalpha(s[i]))
17                 s[i] = tolower(s[i]);
18             else s[i] = ' ';
19         }
20         istringstream sss(s);
21         while(sss>>s1) ss.insert(s1);
22     }
23     set<string>::iterator it = ss.begin();
24     for(;it != ss.end();++it){
25         cout << (*it) <<endl;
26     }
27     return 0;
28 }

 

 

Misha and Changing Handles

 

 

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.

Input

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.

Output

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.

Example

Input
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
Output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 #include <set>
 6 using namespace std;
 7 map<string,string> mp;
 8 set<string> s;
 9 vector<string> v;
10 int main()
11 {
12     int q, ans = 0;
13     cin >> q;
14     string ss,ss1;
15     while(q--){
16         cin >> ss >> ss1;
17         s.insert(ss1);
18         if(!s.count(ss)) {
19             ans ++;
20             v.push_back(ss);
21         }
22         mp[ss] = ss1;
23     }
24 
25     cout << ans << endl;
26     for(int i = 0; i < v.size(); i++){
27         string sss = v[i];
28         while(mp.count(mp[sss])) sss = mp[sss];
29         cout << v[i] << " " << mp[sss] << endl;
30     }
31     return 0;
32 }

 

What Are You Talking About

 

Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him? 

InputThe problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters. 
OutputIn this problem, you have to output the translation of the history book. 
Sample Input

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END

Sample Output

hello, i'm from mars.
i like earth!


 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 using namespace std;
 6 map<string,string> mp;
 7 string s,ss;
 8 int main()
 9 {
10     cin >> s;
11     while(cin>>s){
12         if(s == "END")
13             break;
14         cin>>ss;
15         mp[ss] = s;
16     }
17     //cout << mp["difh"] << endl;
18     cin>>s;
19     getline(cin,s);
20     while(getline(cin,s)){
21         ss = "";
22         if(s == "END")
23             break;
24         ss = "";
25         for(int i = 0; i < s.size(); i++){
26             if(islower(s[i])){
27                 ss+=s[i];
28             }else{
29                 if(mp.find(ss)!=mp.end()){
30                     cout << mp[ss];
31                 }
32                 else cout << ss;
33                 ss = "";
34                 cout << s[i];
35             }
36         }
37         cout << endl;
38     }
39     return 0;
40 }

 

SPY

 

The National Intelligence Council of X Nation receives a piece of credible information that Nation Y will send spies to steal Nation X’s confidential paper. So the commander of The National Intelligence Council take measures immediately, he will investigate people who will come into NationX. At the same time, there are two List in the Commander’s hand, one is full of spies that Nation Y will send to Nation X, and the other one is full of spies that Nation X has sent to Nation Y before. There may be some overlaps of the two list. Because the spy may act two roles at the same time, which means that he may be the one that is sent from Nation X to Nation Y, we just call this type a “dual-spy”. So Nation Y may send “dual_spy” back to Nation X, and it is obvious now that it is good for Nation X, because “dual_spy” may bring back NationY’s confidential paper without worrying to be detention by NationY’s frontier So the commander decides to seize those that are sent by NationY, and let the ordinary people and the “dual_spy” in at the same time .So can you decide a list that should be caught by the Commander? 
A:the list contains that will come to the NationX’s frontier. 
B:the list contains spies that will be sent by Nation Y. 
C:the list contains spies that were sent to NationY before. 

InputThere are several test cases. 
Each test case contains four parts, the first part contains 3 positive integers A, B, C, and A is the number which will come into the frontier. B is the number that will be sent by Nation Y, and C is the number that NationX has sent to NationY before. 
The second part contains A strings, the name list of that will come into the frontier. 
The second part contains B strings, the name list of that are sent by NationY. 
The second part contains C strings, the name list of the “dual_spy”. 
There will be a blank line after each test case. 
There won’t be any repetitive names in a single list, if repetitive names appear in two lists, they mean the same people. 
OutputOutput the list that the commander should caught (in the appearance order of the lists B).if no one should be caught, then , you should output “No enemy spy”Sample Input

8 4 3
Zhao Qian Sun Li Zhou Wu Zheng Wang
Zhao Qian Sun Li
Zhao Zhou Zheng
2 2 2
Zhao Qian
Zhao Qian
Zhao Qian

Sample Output

Qian Sun Li
No enemy spy

 1 #include
 2 using namespace std;
 3  
 4 int main() {
 5     ios::sync_with_stdio(false); cin.tie(0);
 6     int a, b, c;
 7     int w = 0;
 8     string name;
 9     while (cin >> a >> b >> c) {
10         set<string> s;
11         vector<string> v;
12         for (int i=0; i) {
13             cin >> name;
14       //      cout << ++w <
15             s.insert(name);
16         }
17         for (int j=0; j) {
18             cin >> name;
19       //      cout << ++w <
20             if (s.count(name)) v.push_back(name);
21         }
22         for (int k=0; k) {
23             cin >> name;
24         //    cout << ++w <
25             for (int i=0; i) {
26                 if (v[i] == name) {
27                     v.erase(v.begin()+i);
28                 }
29             }
30         }
31         if (v.empty()) cout << "No enemy spy\n";
32         else {
33             int sz = v.size();
34             cout << v[0];
35             for (int i=1; i)
36                 cout << " " << v[i];
37             cout << "\n";
38         }
39     }
40     return 0;
41 }

Bombing

 

It’s a cruel war which killed millions of people and ruined series of cities. In order to stop it, let’s bomb the opponent’s base. 
It seems not to be a hard work in circumstances of street battles, however, you’ll be encountered a much more difficult instance: recounting exploits of the military. In the bombing action, the commander will dispatch a group of bombers with weapons having the huge destructive power to destroy all the targets in a line. Thanks to the outstanding work of our spy, the positions of all opponents’ bases had been detected and marked on the map, consequently, the bombing plan will be sent to you. 
Specifically, the map is expressed as a 2D-plane with some positions of enemy’s bases marked on. The bombers are dispatched orderly and each of them will bomb a vertical or horizontal line on the map. Then your commanded wants you to report that how many bases will be destroyed by each bomber. Notice that a ruined base will not be taken into account when calculating the exploits of later bombers. 

InputMultiple test cases and each test cases starts with two non-negative integer N (N<=100,000) and M (M<=100,000) denoting the number of target bases and the number of scheduled bombers respectively. In the following N line, there is a pair of integers x and y separated by single space indicating the coordinate of position of each opponent’s base. The following M lines describe the bombers, each of them contains two integers c and d where c is 0 or 1 and d is an integer with absolute value no more than 10 9, if c = 0, then this bomber will bomb the line x = d, otherwise y = d. The input will end when N = M = 0 and the number of test cases is no more than 50. 
OutputFor each test case, output M lines, the ith line contains a single integer denoting the number of bases that were destroyed by the corresponding bomber in the input. Output a blank line after each test case.Sample Input

3 2
1 2
1 3
2 3
0 1
1 3
0 0

Sample Output

2
1

 

 1 #include 
 2 #include 
 3 #include <set>
 4 #include 
 5 using namespace std;
 6 map<int,multiset<int> > mx,my;
 7 typedef map<int,multiset<int> > mmpp;
 8 int f(mmpp &x, mmpp &y, int pos){
 9     int ans = x[pos].size();
10     multiset<int>::iterator it = x[pos].begin();
11     for(;it != x[pos].end(); ++it){
12         y[(*it)].erase(pos);
13     }
14     x[pos].clear();
15     return ans;
16 }
17 int main()
18 {
19     int n, m;
20     while(~scanf("%d%d",&n,&m)){
21         if(n == 0 && m == 0)
22             break;
23         int x, y, c, d;
24         for(int i = 0; i < n; i++){
25             scanf("%d%d",&x,&y);
26             mx[x].insert(y);
27             my[y].insert(x);
28         }
29         for(int i = 0; i < m; i++){
30             scanf("%d%d",&c,&d);
31             int ans = 0;
32             if(c == 0) ans = f(mx,my,d);
33             else ans = f(my,mx,d);
34             printf("%d\n",ans);
35         }
36         printf("\n");
37         mx.clear();
38         my.clear();
39     }
40     return 0;
41 }

 

HDU 

Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。
 

 

Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。
 

 

Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。
 

 

Sample Input
you are my friend #
 

 

Sample Output
4
 

 

Author
Lily
注意输出流的用法
 1 #include 
 2 #include 
 3 #include <set>
 4 #include 
 5 using namespace std;
 6 set<string>mp;
 7 int main()
 8 {
 9     string s, sss;
10     while(getline(cin,s)){
11         if(s == "#")
12             break;
13         istringstream ss(s);
14         while(ss>>sss){
15             mp.insert(sss);
16         }
17         cout << mp.size() << endl;
18         mp.clear();
19     }
20     return 0;
21 }

 

转载于:https://www.cnblogs.com/xingkongyihao/p/6535394.html

你可能感兴趣的:(STL学习)