第五章例题

稍微测试了一下cin的用法和字节流的用法

 1 #include <iostream>
 2 #include <string>
 3 #include <set>
 4 #include <sstream>
 5 using namespace std;
 6 
 7 
 8 int main() {
 9 
10 /*
11 
12     //cin>>和ss读字符串时跳过前面的空白符,当再次回车时从相应的缓冲区读入
13 
14  
15     string s="    lanti    anci";
16     string buf;
17 
18     stringstream ss(s);
19 
20     while(ss>>buf)
21         cout<<buf;
22 
23     cout<<endl;
24 
25 */
26 
27     string s;
28     cin>>s;
29 
30     cout<<s<<endl;
31 
32     return 0;
33 }

5-3

 1 #include <iostream>
 2 #include <sstream>
 3 #include <string>
 4 #include <set>
 5 
 6 using namespace std;
 7 
 8 set<string> dict;
 9 
10 int main()
11 {
12     string s,buf;
13 
14     while(cin>>s)
15     {
16         for(unsigned int i=0;i<s.length();i++)
17         {
18             if(isalpha(s[i]))
19                 s[i]=tolower(s[i]);
20             else
21                 s[i]=' ';
22         }
23 
24         stringstream ss(s);
25 
26         ss >> buf; 
27         dict.insert(buf);
28     }
29 
30     for(set<string>::iterator it=dict.begin();it!=dict.end();it++)
31         cout<<*it<<endl;
32 
33     return 0;
34 }

 5-4

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <set>

using namespace std;

vector<string> words_v;
map<string,int> st_w;


string per(string& st)
{
    string temp=st;

    for(unsigned int i=0;i<st.length();i++)
        temp[i]=tolower(temp[i]);

    sort(temp.begin(),temp.end());

    return temp;
}


int main()
{
    string word;

    while(cin>>word)
    {    
        if(word[0]=='#')
            break;

        words_v.push_back(word);
        string temp=per(word);

        if(!st_w.count(temp))
            st_w[temp]=1;
        else
            st_w[temp]++;
    }

    set<string> ans;

    for(unsigned int vit=0;vit<words_v.size();vit++)
        if(st_w[per(words_v[vit])]==1)
            ans.insert(words_v[vit]);

    //sort(ans.begin(),ans.end());    

    for(set<string>::iterator it = ans.begin(); it != ans.end(); ++it)
        cout<<*it<<endl;    

    return 0;    
}

 

5-5

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <stack>
 4 #include <set>
 5 #include <map>
 6 #include <vector>
 7 #include <algorithm>
 8 
 9 using namespace std;
10 
11 #define ALL(x) x.begin(),x.end()
12 #define INS(x) inserter(x,x.begin())                       //注意宏的括号和inserter 
13 
14 typedef set<int> Set;
15 map<Set,int> IDcache;
16 vector<Set> Setcache;
17 stack<int> s;
18 
19 
20 int ID(Set x)
21 {
22     if(IDcache.count(x))
23         return IDcache[x];                                //如果存在集合就返回对应的值
24     Setcache.push_back(x);
25     return IDcache[x]=Setcache.size()-1;                  //不存在该集合,就加入Setcache中然后返回对应的值
26 }
27 
28 
29 int main()
30 {
31     int n;
32     cin>>n;
33 
34     while(n--)
35     {
36         int m;
37         cin>>m;
38 
39         for(int i=0;i<m;i++)
40         {
41             string op;
42             cin>>op;
43 
44             if(op[0]=='P')
45                 s.push(ID(Set()));                       //如果是PUSH就获得其对应的ID压栈
46             else if(op[0]=='D')
47                 s.push(s.top());                         //如果是DUP就把栈顶元素在压入栈中
48             else
49             {
50                 Set x1=Setcache[s.top()];
51                 s.pop();                                 
52                 Set x2=Setcache[s.top()];
53                 s.pop();
54                 Set x;
55                 if(op[0]=='U')
56                     set_union(ALL(x1),ALL(x2),INS(x));                         //并集
57                 if(op[0]=='I')
58                     set_intersection(ALL(x1),ALL(x2),INS(x));                  //交集
59                 if(op[0]=='A')
60                 {
61                     x=x2;
62                     x.insert(ID(x1));                                         //插入元素
63                 }
64                 s.push(ID(x));
65             }
66 
67             cout<<Setcache[s.top()].size()<<endl;
68         }
69 
70         cout<<"***"<<endl;
71     }
72 }

 

5-6

 1 #include <iostream>
 2 #include <queue>
 3 #include <map>
 4 
 5 using namespace std;
 6 
 7 const int maxn=1000;
 8 
 9 queue<int> bigq;
10 queue<int> myqueue[maxn];
11 map<int,int> team;
12 
13 int main()
14 {
15 
16     /*输入每个团队中每个人的编号*/
17     int t;
18     cin>>t;
19 
20     while(t--)
21     {    
22         cout<<"***"<<endl;
23         
24         int queID;
25         cin>>queID;
26 
27         int n;
28         cin>>n;
29         for(int i=0;i<n;i++)
30         {
31             int memID;
32             cin>>memID;
33             team[memID]=queID;
34         }
35     }
36 
37     /*模拟操作*/
38 
39     for(;;)
40     {
41         string op;
42         
43         cin>>op;
44 
45         if(op[0]=='S')
46             break;
47 
48 
49         int memID;
50         int queID;
51 
52         if(op[0]=='E')
53         {
54             cin>>memID;
55 
56             queID=team[memID];
57 
58             if(myqueue[queID].empty())
59             {
60                 bigq.push(queID);
61                 myqueue[queID].push(memID);
62             }
63             else
64             myqueue[queID].push(memID);
65         }
66 
67         if(op[0]=='D')
68         {
69             queID=bigq.front();
70 
71             memID=myqueue[queID].front();
72             myqueue[queID].pop();
73 
74             if(myqueue[queID].empty())
75                 bigq.pop();
76 
77             cout<<memID<<endl;
78         }
79 
80     }
81 
82     return 0;
83 }

 

5-7

 1 #include <iostream>
 2 #include <queue>
 3 #include <map>
 4 
 5 using namespace std;
 6 
 7 const int maxn=1000;
 8 
 9 queue<int> bigq;
10 queue<int> myqueue[maxn];
11 map<int,int> team;
12 
13 int main()
14 {
15 
16     /*输入每个团队中每个人的编号*/
17     int t;
18     cin>>t;
19 
20     while(t--)
21     {    
22         cout<<"***"<<endl;
23         
24         int queID;
25         cin>>queID;
26 
27         int n;
28         cin>>n;
29         for(int i=0;i<n;i++)
30         {
31             int memID;
32             cin>>memID;
33             team[memID]=queID;
34         }
35     }
36 
37     /*模拟操作*/
38 
39     for(;;)
40     {
41         string op;
42         
43         cin>>op;
44 
45         if(op[0]=='S')
46             break;
47 
48 
49         int memID;
50         int queID;
51 
52         if(op[0]=='E')
53         {
54             cin>>memID;
55 
56             queID=team[memID];
57 
58             if(myqueue[queID].empty())
59             {
60                 bigq.push(queID);
61                 myqueue[queID].push(memID);
62             }
63             else
64             myqueue[queID].push(memID);
65         }
66 
67         if(op[0]=='D')
68         {
69             queID=bigq.front();
70 
71             memID=myqueue[queID].front();
72             myqueue[queID].pop();
73 
74             if(myqueue[queID].empty())
75                 bigq.pop();
76 
77             cout<<memID<<endl;
78         }
79 
80     }
81 
82     return 0;
83 }

 

posted @ 2017-07-19 11:49  lan126  阅读(168)  评论(0编辑  收藏  举报