POJ3297+map字符串处理

简单的字符串处理题。

要注意细节!

附数据两组:

ABCC
abc
ae
AAA
abc
AAAA
abc

A
a
B
a
C
a

 1 /*
 2 
 3 */
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<stdlib.h>
 7 #include<algorithm>
 8 #include<iostream>
 9 #include<queue>
10 #include<map>
11 #include<stack>
12 #include<set>
13 #include<math.h>
14 using namespace std;
15 typedef long long int64;
16 //typedef __int64 int64;
17 typedef pair<int64,int64> PII;
18 #define MP(a,b) make_pair((a),(b)) 
19 const int maxn = 105;
20 const int maxm = 1005;
21 const int inf = 0x7fffffff;
22 const double pi=acos(-1.0);
23 const double eps = 1e-8;
24 
25 struct Node{
26     string name;
27     int cnt;
28 }project[ maxn ];
29 map<string,int>mp;
30 map<string,int>special;
31 void init(){
32     mp.clear();
33     special.clear();
34 }
35 int cmp( Node a,Node b ){
36     if( a.cnt!=b.cnt )
37         return a.cnt>b.cnt;
38     else
39         return a.name<b.name;
40 }
41 int main(){ 
42     string temp;
43     while( getline( cin,temp ) ){
44         //cout<<"temp = "<<temp<<endl;
45         if( '0'==temp[0] ) 
46             break;
47         if( '1'==temp[0] ) 
48             continue;
49         init();
50         int Num = 1;
51         while( 1 ){
52             project[ Num ].name = temp;
53             project[ Num ].cnt = 0;
54             while( 1 ){
55                 getline( cin,temp );
56                 //cout<<"temp = "<<temp<<endl;
57                 if( temp.size()==1&&temp[0]=='1' ) break;
58                 if( (  temp[0]>='a'&&temp[0]<='z' )||( temp[0]>='0'&&temp[0]<='9' ) ){
59                     if( mp[ temp ]==0 ){
60                         mp[ temp ] = Num;
61                         project[ Num ].cnt ++ ;
62                     }
63                     else {
64                         if( mp[ temp ]==Num ){}
65                         else if( special[ temp ]==0 ){
66                             special[ temp ] = 1;
67                             if( project[ mp[temp] ].cnt>0 ) 
68                                 project[ mp[ temp ] ].cnt --;
69                         }
70                     }
71                 }
72                 else
73                     break;
74             }
75             Num ++ ;
76             if( '1'==temp[0] ) break;
77         }
78         //cout<<"ok"<<endl;
79         sort( project+1,project+Num,cmp );
80         for( int i=1;i<Num;i++ ){
81             cout<<project[i].name<<" "<<project[i].cnt<<endl;
82         }
83     }
84     return 0;
85 }
View Code
keep moving...
原文地址:https://www.cnblogs.com/xxx0624/p/3308661.html