pku 1002
submit了十几次了,终于AC了,用c++ stl map+string做的
1 #include <iostream>
2 #include <map>
3 #include <string>
4 using namespace std;
5
6 string format(char *s)
7 {
8 int i,index;
9 char r[20];
10 index = 0;
11 i=0;
12 while (s[i]!='\0')
13 {
14 if(s[i]>='A'&&s[i]<='R')
15 r[index++] = (s[i] - 'A')/3 + '2';
16 else if(s[i]>='S'&&s[i]<='Y')
17 {
18 r[index++] = (s[i] - 'A' - 1)/3 + '2';
19 }
20 else if(s[i]!='-')
21 r[index++] = s[i];
22 if(index==3)
23 r[index++] = '-';
24 i++;
25 }
26 r[index] = '\0';
27 string str(r);
28 return str;
29 }
30
31 int main()
32 {
33 int i,n;
34 char s[20];
35 string str;
36 map <string,int> m;
37 map <string,int>::const_iterator m_it;
38 int flag = 0;
39 cin>>n;
40 for (i=0; i<n; i++)
41 {
42 cin>>s;
43 str=format(s);
44 if (m.find(str)!=m.end())
45 {
46 m[str]++;
47 }
48 else
49 m[str]=1;
50 }
51 m_it = m.begin();
52
53 while (m_it!= m.end())
54 {
55 if(m_it->second>1)
56 {
57 flag = 1;
58 cout<<m_it->first<<" "<<m_it->second<<endl;
59 }
60 m_it++;
61 }
62 if(!flag)
63 cout<<"No duplicates."<<endl;
64 return 0;
2 #include <map>
3 #include <string>
4 using namespace std;
5
6 string format(char *s)
7 {
8 int i,index;
9 char r[20];
10 index = 0;
11 i=0;
12 while (s[i]!='\0')
13 {
14 if(s[i]>='A'&&s[i]<='R')
15 r[index++] = (s[i] - 'A')/3 + '2';
16 else if(s[i]>='S'&&s[i]<='Y')
17 {
18 r[index++] = (s[i] - 'A' - 1)/3 + '2';
19 }
20 else if(s[i]!='-')
21 r[index++] = s[i];
22 if(index==3)
23 r[index++] = '-';
24 i++;
25 }
26 r[index] = '\0';
27 string str(r);
28 return str;
29 }
30
31 int main()
32 {
33 int i,n;
34 char s[20];
35 string str;
36 map <string,int> m;
37 map <string,int>::const_iterator m_it;
38 int flag = 0;
39 cin>>n;
40 for (i=0; i<n; i++)
41 {
42 cin>>s;
43 str=format(s);
44 if (m.find(str)!=m.end())
45 {
46 m[str]++;
47 }
48 else
49 m[str]=1;
50 }
51 m_it = m.begin();
52
53 while (m_it!= m.end())
54 {
55 if(m_it->second>1)
56 {
57 flag = 1;
58 cout<<m_it->first<<" "<<m_it->second<<endl;
59 }
60 m_it++;
61 }
62 if(!flag)
63 cout<<"No duplicates."<<endl;
64 return 0;
65 }