现在STL对我熟了,我对它还不熟,加油啊
这种循环输入的不清空容器则要在主函数中定义……
map容器
运用STL,由于map<string,int>存储是按KEY值的字母顺序排序,所以这里呢省去了排序的步骤
HDU 1263
- #include<iostream>
- #include<cstdio>
- #include<string>
- #include<map>
- using namespace std;
- int main()
- {
- map<string,map<string,int> > p;
- map<string,int> q;
- map<string,map<string,int> >::iterator i;
- map<string,int>::iterator j;
- int n,m,t;
- string str1,str2;
- cin>>n;
- while(n--)
- {
- p.clear();
- q.clear();
- cin>>m;
- while(m--)
- {
- cin>>str1>>str2>>t;
- p[str2][str1]+=t;
- }
- for(i=p.begin();i!=p.end();i++)
- {
- cout<<i->first<<endl;
- q=i->second;
- for(j=q.begin();j!=q.end();j++)
- cout<<" |----"<<j->first<<"("<<j->second<<")"<<endl;
- }
- if(n)
- cout<<endl;
- }
- }
#include<iostream>
#include<cstdio>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string,map<string,int> > p;
map<string,int> q;
map<string,map<string,int> >::iterator i;
map<string,int>::iterator j;
int n,m,t;
string str1,str2;
cin>>n;
while(n--)
{
p.clear();
q.clear();
cin>>m;
while(m--)
{
cin>>str1>>str2>>t;
p[str2][str1]+=t;
}
for(i=p.begin();i!=p.end();i++)
{
cout<<i->first<<endl;
q=i->second;
for(j=q.begin();j!=q.end();j++)
cout<<" |----"<<j->first<<"("<<j->second<<")"<<endl;
}
if(n)
cout<<endl;
}
}
HDU1075这个题目是对字符串的处理(用字典树也可以),具体就是会判断初始终止条件,熟练运用map容器,但是显然我还做不到,会继续学习STL,我要和你混熟
- #include<iostream>
- #include<string>
- #include<map>
- using namespace std;
- //const int N 3005;
- map<string,string> mapp;
- bool Char(char ch)
- {
- if(ch>='a'&&ch<='z')
- return true;
- return false;
- }
- int main()
- {
- string str1,str2;
- cin>>str1;//忽略start
- while(cin>>str1)
- {
- if(str1=="END") break;
- cin>>str2;
- mapp[str2]=str1;
- }
- cin>>str1;
- getline(cin,str1);
- while(getline(cin,str1))
- {
- str2="";//空
- if(str1=="END") break;
- for(int i=0;i<str1.size();i++)
- {
- if(Char(str1[i]))//如果是字母的话就存上
- str2+=str1[i];
- else
- {
- if(mapp[str2]!="")
- cout<<mapp[str2];
- else
- cout<<str2;
- cout<<str1[i];
- str2="";
- }
- }
- cout<<endl;
- }
- return 0;
- }
#include<iostream>
#include<string>
#include<map>
using namespace std;
//const int N 3005;
map<string,string> mapp;
bool Char(char ch)
{
if(ch>='a'&&ch<='z')
return true;
return false;
}
int main()
{
string str1,str2;
cin>>str1;//忽略start
while(cin>>str1)
{
if(str1=="END") break;
cin>>str2;
mapp[str2]=str1;
}
cin>>str1;
getline(cin,str1);
while(getline(cin,str1))
{
str2="";//空
if(str1=="END") break;
for(int i=0;i<str1.size();i++)
{
if(Char(str1[i]))//如果是字母的话就存上
str2+=str1[i];
else
{
if(mapp[str2]!="")
cout<<mapp[str2];
else
cout<<str2;
cout<<str1[i];
str2="";
}
}
cout<<endl;
}
return 0;
}
HDU 1004(非STL解法见1004传送门)
- #include<iostream>
- #include<map>
- #include<string>
- using namespace std;
- int main()
- {
- int n;
- string str;
- while(cin>>n,n)
- {
- map<string,int>mapp;
- map<string,int>:: iterator pr;
- for(int i=0;i<n;i++)
- {
- cin>>str;
- mapp[str]++;//将元素存入map容器中
- }
- int maxn=0;
- for(pr = mapp.begin();pr != mapp.end();pr++)
- {
- if(pr->second > maxn)
- {
- maxn = pr->second;
- str = pr->first;
- }
- }
- cout<<str<<endl;
- }
- return 0;
- }
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main()
{
int n;
string str;
while(cin>>n,n)
{
map<string,int>mapp;
map<string,int>:: iterator pr;
for(int i=0;i<n;i++)
{
cin>>str;
mapp[str]++;//将元素存入map容器中
}
int maxn=0;
for(pr = mapp.begin();pr != mapp.end();pr++)
{
if(pr->second > maxn)
{
maxn = pr->second;
str = pr->first;
}
}
cout<<str<<endl;
}
return 0;
}
ZOJ 2724
用结构体存储各元素,优先队列存储存储结构体
- #include<iostream>//zoj 2724
- #include<queue>
- #include<string>
- #include<cstdio>
- using namespace std;
- struct Compare
- {
- char str[30];
- int m;
- int pr;//优先级
- friend bool operator <(Compare a,Compare b)
- {
- return a.pr>b.pr;
- }
- };
- priority_queue<Compare>it;
- int main()
- {
- Compare pluss;
- string str1;
- while(cin>>str1)
- {
- if(str1=="GET")
- {
- if(it.empty())
- {
- cout<<"EMPTY QUEUE!"<<endl;
- continue;
- }
- pluss=it.top();
- it.pop();//移除这个元素
- cout<<pluss.str<<" "<<pluss.m<<endl;
- }
- if(str1=="PUT")
- {
- scanf("%s%d%d",pluss.str,&pluss.m,&pluss.pr);
- it.push(pluss);
- }
- }
- return 0;
- }
#include<iostream>//zoj 2724
#include<queue>
#include<string>
#include<cstdio>
using namespace std;
struct Compare
{
char str[30];
int m;
int pr;//优先级
friend bool operator <(Compare a,Compare b)
{
return a.pr>b.pr;
}
};
priority_queue<Compare>it;
int main()
{
Compare pluss;
string str1;
while(cin>>str1)
{
if(str1=="GET")
{
if(it.empty())
{
cout<<"EMPTY QUEUE!"<<endl;
continue;
}
pluss=it.top();
it.pop();//移除这个元素
cout<<pluss.str<<" "<<pluss.m<<endl;
}
if(str1=="PUT")
{
scanf("%s%d%d",pluss.str,&pluss.m,&pluss.pr);
it.push(pluss);
}
}
return 0;
}