【算法竞赛入门经典第五章:C++与STL入门
2018-02-14 20:45
615 查看
5.2.3集合set
例题5.3 安迪的第一个字典#include<iostream> #include<string> #include<set> #include<sstream> using namespace std; set<string> dict; int main(){ string s,buf; while(cin >> s){ for(int i=0;i<s.length();i++) if(isalpha(s[i])) s[i]=tolower(s[i]); else s[i]=' '; stringstream ss(s); while(ss>>buf) dict.insert(buf); } for(set<string>::iterator it =dict.begin();it != dict.end();it++) cout<<*it<<endl; return 0; }
笔记:其中包含着几个要点:1.isalpha函数,tolower函数,stringstream,还有核心的set问题。
5.2.4:映射:map
例题5.4 反片语#include<iostream> #include<string> #include<cctype> #include<vector> #include<map> #include<algorithm> using namespace std; map<string,int> cnt; vector<string> words; string repr(const string &s){ string ans = s; for(int i=0;i<ans.length();i++) ans[i]=tolower(ans[i]); sort(ans.begin(),ans.end()); return ans; } int main(){ int n=0; string s; while(cin>>s){ if(s[0]=='#') break; words.push_back(s); string r=repr(s); if(!cnt.count(r)) cnt[r]=0; cnt[r]++; } vector<string> ans; for(int i=0;i<words.size();i++) if(cnt[repr(words[i])]==1) ans.push_back(words[i]); sort(ans.begin(),ans.end()); for(int i=0;i<ans.size();i++) cout<<ans[i]<<endl; return 0; }笔记:map的用法可以理解为,更为高级的数组。
相关文章推荐
- 刘汝佳 算法竞赛入门经典 第5章 C++与STL入门
- 算法之路二:刘汝佳算法竞赛入门经典:STL队列 团体队列UVA540
- 算法竞赛入门经典-第五章因子和阶乘
- 算法竞赛入门经典 第二章 上机练习(C++代码)
- 算法之路二:刘汝佳算法竞赛入门经典:STL集合 安迪的第一个字典 UVA10851
- 算法竞赛入门经典 第五章 字符串
- 算法竞赛入门经典:第五章 基础题目选解 5.3 周期串
- 算法竞赛入门经典第五章例题5-5 The SetStack Computer UVA - 12096
- 算法竞赛入门经典第五章习题
- STL + c++ + 模板 + 重要思维 + 基础算法+ 经典算法 + 经典实例 + 编程总结+ 心得+ 入门必会 + 知识点汇总。+string +dfs +bfs等重要算法
- 算法竞赛入门经典-第五章6174问题
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 2(Big Number)
- 算法之路二:刘汝佳算法竞赛入门经典:STL排序与检索 大理石在哪 UVA10474
- C++ 算法竞赛入门经典 回文串,镜像串问题
- 算法竞赛入门经典-第五章TEX括号
- 随机生成数,摘自算法竞赛入门经典P120-P123测试STL。
- 算法竞赛入门经典第五章例题5-7 Ugly Numbers UVA - 136
- 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 1(String)
- 算法竞赛入门经典课后习题答案:第一章(C++解法)
- 算法竞赛入门经典:第五章 基础题目选解 5.9 Cantor数