map
Let the Balloon Rise
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 113243 Accepted Submission(s): 44255
A test case with N = 0 terminates the input and this test case is not to be processed.
5 green red blue red red 3 pink orange pink 0
red pink
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
map<string,int >cnt;
int main()
{
int n;
cnt.clear(); //清空map;
while(cin>>n)
{
if(n==0) break;
string s;
for(int i=0;i<n;i++)
{
cin>>s;
if(!cnt.count(s)) //查询map中是否有字符串s;
cnt[s]=1;
else
cnt[s]++;
}
int maxx=0;
map<string ,int>::iterator it,tmp; //map的迭代;
for(it=cnt.begin();it!=cnt.end();it++)
{
if(it->second>maxx)
{
maxx=it->second; //调用map;
tmp=it;
}
}
cout<<tmp->first<<endl;
cnt.clear();
}
return 0;
}
map与pair函数;
Long time ago Alex created an interesting problem about parallelogram. The input data for this problem contained four integer points on the Cartesian plane, that defined the set of vertices of some non-degenerate (positive area) parallelogram. Points not necessary were given in the order of clockwise or counterclockwise traversal.
Alex had very nice test for this problem, but is somehow happened that the last line of the input was lost and now he has only three out of four points of the original parallelogram. He remembers that test was so good that he asks you to restore it given only these three points.
The input consists of three lines, each containing a pair of integer coordinates xi and yi ( - 1000 ≤ xi, yi ≤ 1000). It's guaranteed that these three points do not lie on the same line and no two of them coincide.
First print integer k — the number of ways to add one new integer point such that the obtained set defines some parallelogram of positive area. There is no requirement for the points to be arranged in any special order (like traversal), they just define the set of vertices.
Then print k lines, each containing a pair of integer — possible coordinates of the fourth point.
0 0 1 0 0 1
3 1 -1 -1 1 1 1
If you need clarification of what parallelogram is, please check Wikipedia page:
https://en.wikipedia.org/wiki/Parallelogram
代码:
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
set<pair<int,int > >s; //定义set,用到了pair函数;
/*pair的应用
pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,
如stl中的map就是将key和value放在一起来保存。
另一个应用是,当一个函数需要返回2个数据的时候,可以选择pair。
pair的实现是一个结构体,主要的两个成员变量是first second
因为是使用struct不是class,所以可以直接使用pair的成员变量。*/
int main()
{
int x1,y1,x2,y2,x3,y3;
cin>>x1>>y1>>x2>>y2>>x3>>y3;
s.insert(make_pair(x1+x2-x3,y1+y2-y3));
s.insert(make_pair(x1+x3-x2,y1+y3-y2));
s.insert(make_pair(x2+x3-x1,y2+y3-y1));
cout<<s.size()<<endl;
set<pair<int,int> >::iterator it;
for(it=s.begin();it!=s.end();it++)
cout<<it->first<<" "<<it->second<<endl;
return 0;
}
于本题而言,k恒为3;
One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into several parts. This time Lesha decided to split the array A into several, possibly one, new arrays so that the sum of elements in each of the new arrays is not zero. One more condition is that if we place the new arrays one after another they will form the old array A.
Lesha is tired now so he asked you to split the array. Help Lesha!
The first line contains single integer n (1 ≤ n ≤ 100) — the number of elements in the array A.
The next line contains n integers a1, a2, ..., an ( - 103 ≤ ai ≤ 103) — the elements of the array A.
If it is not possible to split the array A and satisfy all the constraints, print single line containing "NO" (without quotes).
Otherwise in the first line print "YES" (without quotes). In the next line print single integer k — the number of new arrays. In each of the next k lines print two integers li and ri which denote the subarray A[li... ri] of the initial array A being the i-th new array. Integers li, rishould satisfy the following conditions:
- l1 = 1
- rk = n
- ri + 1 = li + 1 for each 1 ≤ i < k.
If there are multiple answers, print any of them.
3 1 2 -3
YES 2 1 2 3 3
8 9 -12 3 4 -4 -10 7 3
YES 2 1 2 3 8
1 0
NO
4 1 2 3 -5
YES 4 1 1 2 2 3 3 4 4
大意:给你一个数组,问是否能找到元素和不为 0 的子数组。
思路:只要原数组不全为 0 就肯定 YES 啊,然后不为 0 的元素自己就是一个(只存在一个元素)数组,碰到 0 的元素,就跟下一个不为的 0 的元素合一块作为一个数组就行了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef pair<int,int> pii;
int n;
int a[110];
pii ans[110];
int main()
{
while(~scanf("%d",&n))
{
int cnt=0,k=0,p=1;
for(int i=1;i<=n;i++)
{
scanf("%d",a+i);
if(a[i]!=0)
{
ans[k].first=p;
ans[k++].second=i;
p=i+1;
}
}
if(p!=n+1) // 说明末尾有存在一定数量的 0
ans[k-1].second=n;
if(k==0)
{
puts("NO");
}
else
{
puts("YES");
printf("%d\n",k);
for(int i=0;i<k;i++)
printf("%d %d\n",ans[i].first,ans[i].second);
}
}
return 0;
}
/*
5
1 -1 1 -1 1
*/
可用map习题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<algorithm>
using namespace std;
map<char ,char >cnt1;
map<char ,char >cnt2;
char s1[1003],s2[1003];
int main()
{
int same1,same2,flag;
while(~scanf("%s%s",s1,s2))
{
cnt1.clear(),cnt2.clear();
same1=0,same2=0,flag=0;
int len=strlen(s1);
for(int i=0; i<len; i++)
{
char ss1,ss2,t;
ss1=s1[i],ss2=s2[i];
if(ss1>ss2)
{
t=ss1;
ss1=ss2;
ss2=t;
}
if((cnt1[ss1]!=NULL)&&cnt1[ss1]!=ss2)
flag=1;
else
cnt1[ss1]=ss2;
if((cnt2[ss2]!=NULL)&&cnt2[ss2]!=ss1)
flag=1;
else
cnt2[ss2]=ss1;
}
map<char ,char>::iterator it1,it2,its;
// for(it=cnt.begin(); it!=cnt.end(); it++)
// cout<<it->first<<" "<<it->second<<" nani"<<endl;
// printf("store ok!\n");
//再开一个map;
for(it1=cnt1.begin(); it1!=cnt1.end(); it1++)
{
if(it1->first==it1->second)
same1++;
}
for(it2=cnt2.begin();it2!=cnt2.end();it2++)
{
if(it2->first==it2->second)
same2++;
}
its=cnt1.begin(); its++;
for(it1=cnt1.begin(); its!=cnt1.end(); its++,it1++)
{
if(its->first==it1->second||its->second==it1->second)
flag=1;
}
its=cnt2.begin(); its++;
for(it2=cnt2.begin(); its!=cnt2.end(); its++,it2++)
{
if(its->first==it2->second||its->second==it2->second)
flag=1;
}
if(cnt1.size()!=cnt2.size())
flag=1;
if(flag)
{
printf("-1\n");
}
else
{
int m=cnt1.size()-same1;
cout<<m<<endl;
for(it1=cnt1.begin(); it1!=cnt1.end(); it1++)
{
if(it1->first==it1->second)
continue;
else
cout<<it1->first<<" "<<it1->second<<endl;
}
}
}
return 0;
}
set
{A} + {B}
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19006 Accepted Submission(s): 7983
注:同一个集合中不会有两个相同的元素.
1 2 1 2 3 1 2 1 1 2
1 2 3 1 2
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MOD=7;
const int N=5;
int main()
{
set<int >s; //定义集合set;
int n,m;
while(cin>>n>>m)
{
int x;
for(int i=0;i<n;i++)
{
scanf("%d",&x);
s.insert(x); //插入元素s;
}
for(int i=0;i<m;i++)
{
scanf("%d",&x);
s.insert(x);
}
set<int>::iterator Ite=s.begin(); //定义set迭代器;
cout<<*Ite; //输出set中元素;
Ite++;
for(;Ite!=s.end();Ite++)
cout<<" "<<*Ite;
cout<<endl;
s.clear(); //清空set;
}
return 0;
}
单词数
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 48101 Accepted Submission(s): 11728
you are my friend #
4
#include<iostream>
#include<cstdio>
#include<set>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1000000;
char w[N];
int main()
{
int len;
set<string >words;
while(gets(w)!=NULL)
{
words.clear();
string s;
int len=strlen(w);
if(w[0]=='#')
break;
for(int i=0;i<len;i++)
{
if(w[i]==' '&&!s.empty())
{
words.insert(s);
s.clear();
}
else if(w[i]!=' ')
s+=w[i];
}
if(!s.empty()) words.insert(s);
cout<<words.size()<<endl;
// set<string >::iterator it;
// for(it=words.begin();it!=words.end();it++)
// cout<<"|"<<*it<<"|"<<" ";
// cout<<endl;
}
return 0;
}