您的位置:首页 > 编程语言 > C语言/C++

UVa 1592 数据库(c++pair)

2016-11-21 23:18 323 查看

Input

Inputcontainsseveraldatasets.Thefirstlineofeachdatasetcontainstwointegernumbersnandm(1

n

10000,1

m

10),thenumberofrowsandcolumnsinthetable.Thefollowingnlinescontaintablerows.Eachrowhasmcolumnvaluesseparatedbycommas.ColumnvaluesconsistofASCIIcharactersfromspace(ASCIIcode32)totilde(ASCIIcode126)withtheexceptionofcomma(ASCIIcode44).Valuesarenotemptyandhavenoleadingandtrailingspaces.Eachrowhasatmost80characters(includingseparatingcommas).

Output

Foreachdataset,ifthetableisinPNFwritetotheoutputfileasingleword``YES"(withoutquotes).IfthetableisnotinPNF,thenwritethreelines.Onthefirstlinewriteasingleword``NO"(withoutquotes).Onthesecondlinewritetwointegerrownumbersr1andr2(1

r1,r2

n,r1

r2),onthethirdlinewritetwointegercolumnnumbersc1andc2(1

c1,c2

m,c1

c2),sothatvaluesincolumnsc1andc2arethesameinrowsr1andr2.

SampleInput

33
HowtocompeteinACMICPC,Peter,peter@neerc.ifmo.ru
HowtowinACMICPC,Michael,michael@neerc.ifmo.ru
NotesfromACMICPCchampion,Michael,michael@neerc.ifmo.ru
23
1,Peter,peter@neerc.ifmo.ru
2,Michael,michael@neerc.ifmo.ru

SampleOutput

NO
23
23
YES

这道题应该使用map来对数据做个一一映射,先将输入的字符串通过map映射成数字,再输入到一个数组里。
之后把c1,c2两列的内容作为一个二元组存到一个map中,由于需要存入两个数据,在这里可以使用pair,在map中就是map<pair<int,int>,int>。
这儿写一下pair的用法:

makr_pair:

pair<int,int>p(5,6);

pair<int,int>p1=make_pair(5,6);

pair<string,double>p2("aa",5.0);

pair<string,double>p3=make_pair("aa",5.0);

有这两种写法来生成一个pair。

如何取得pair的值呢。。

每个pair都有两个属性值first和second

cout<<p1.first<<p1.second;

注意是属性值而不是方法。



#include<iostream>
#include<map>
#include<string>

usingnamespacestd;

map<string,int>IDcache;
map<pair<int,int>,int>NewIDcache;
inta[11000][20];
intn,m;

voidmatch()
{
intx,y;
for(intc1=0;c1<m-1;c1++)
{
for(intc2=c1+1;c2<m;c2++)
{
for(intr=0;r<n;r++)
{
x=a[r][c1];
y=a[r][c2];
if(!NewIDcache.count(make_pair(x,y)))NewIDcache[make_pair(x,y)]=r;
else
{
cout<<"NO"<<endl;
cout<<NewIDcache[make_pair(x,y)]+1<<""<<r+1<<endl;
cout<<c1+1<<""<<c2+1<<endl;
return;
}
}
NewIDcache.clear();
}
}
cout<<"YES"<<endl;
}

intmain()
{
while(cin>>n>>m)
{
intt=1;
getchar();
stringstr;
IDcache.clear();
NewIDcache.clear();
for(inti=0;i<n;i++)
{
intcount=0;
str.clear();
getline(cin,str);
intl=str.length();
strings;
for(intj=0;j<l;j++)
{
if(str[j]!=',')s=s+str[j];
if(str[j]==','||j==l-1)
{
if(!IDcache.count(s))IDcache[s]=t++;
a[i][count++]=IDcache[s];
s.clear();
}
}
}
match();
}
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: