【HDU 3242 List Operations】 字符串处理

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3242

 

题目大意:给你两个集合,A,B,让你实现操作A+B,A-B(集合内能存相同的)。

 

解题思路:

开始脑抽了,我居然用遍历进行判断一个一个去标记去判断,完全的胡搞,各种测试各种对,wrong answer到死,下次再来检查。

过不了,果断换一种写法。用vector去存储相加删除,代码简洁清晰,汗颜,开始怎么没想到呢。

 

AC代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <algorithm>
 5 #include <cstring>
 6 #include <string>
 7 using namespace std;
 8 
 9 vector<string>vt;
10 vector<string>::iterator it;
11 string str1, str2, op;
12 
13 void Push(string str)
14 {
15     vt.clear();
16     int len=str.size();
17     string s="";
18     for(int i=1; i<len; i++)
19     {
20         if(str[i]==','|| str[i]==']')
21         {
22             if(s!="") vt.push_back(s);
23             s="";
24         }
25         else s+=str[i];
26     }
27 }
28 
29 void Add(string str)
30 {
31     int len=str.size();
32     string s="";
33     for(int i=1; i<len; i++)
34     {
35         if(str[i]==','||str[i]==']')
36         {
37             if(s!="") vt.push_back(s);
38             s="";
39         }
40         else s+=str[i];
41     }
42 }
43 
44 void Sub(string str)
45 {
46     int len=str.size();
47     string s="";
48     for(int i=1; i<len; i++)
49     {
50         if(str[i]==','||str[i]==']')
51         {
52             for(it=vt.begin(); it!=vt.end(); it++)
53             {
54                 if(*it==s)
55                 {
56                     vt.erase(it); break;
57                 }
58             }
59             s="";
60         }
61         else s+=str[i];
62     }
63 }
64 
65 int main()
66 {
67     while(cin >> str1)
68     {
69         if(str1==".") break;
70         vt.clear();
71         cin >> op >> str2;
72         Push(str1);
73         if(op=="++") Add(str2);
74         else  Sub(str2);
75         if(vt.begin()==vt.end()){cout << "[]" <<endl; continue;} //特别注意空集合的判断
76         cout << "[";
77         for(it=vt.begin(); it!=vt.end(); it++)
78         {
79             if((it+1)!=vt.end()) cout << *it << "," ;
80             else cout << *it << "]" <<endl;
81         }
82     }
83     return 0;
84 }

 

还没过的代码,下次再来调试。

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     char s1[80], ch[5], s2[80];
10     int  color[80];
11     while(~scanf("%s",s1))
12     {
13         if(s1[0]=='.')  break;
14         scanf("%s%s",ch,s2);
15         memset(color,0,sizeof(color));
16         int lenb=strlen(s2);
17         int lena=strlen(s1);
18         if(ch[0]=='-')
19         {
20             if(lena==2)
21             {
22                 cout << "[]" <<endl;
23                 continue;
24             }
25             int i=1;
26             while(i<lenb)
27             {
28                 if(s2[i]==','||s2[i]==']') { i++; continue; }
29                 else if('0'<=s2[i]&&s2[i]<='9')
30                 {
31                     int tmp=s2[i]-'0';
32                     i++;
33                     while('0'<=s2[i]&&s2[i]<='9')
34                     {
35                         tmp=tmp*10+s2[i]-'0';
36                         i++;
37                     }
38                     int j=1;
39                     while(j<lena)
40                     {
41                         if(color[j]||s1[j]==','||s1[j]==']'||('a'<=s1[j]&&s1[j]<='z')){j++; continue;}
42                         int st=j;
43                         int sum=s1[j]-'0';
44                         j++;
45                         while('0'<=s1[j]&&s1[j]<='9')
46                         {
47                             sum=sum*10+s1[j]-'0';
48                             j++;
49                         }
50                         if(tmp==sum)
51                         {
52                             if(s1[st-1]==',') color[st-1]=1;
53                             for(int k=st; k<j; k++)
54                                 color[k]=1;
55                             break;
56                         }
57                     }
58                 }
59                 else if('a'<=s2[i]&&s2[i]<='z')
60                 {
61                     for(int j=1; j<lena; j++)
62                     {
63                         if(!color[j]&&s2[i]==s1[j])
64                         {
65                             if(s1[j-1]==',') color[j-1]=1;
66                             color[j]=1;
67                             break;
68                         }
69                     }
70                     i++;
71                 }
72             }
73             char pre='#';
74             int ans=0;
75             for(int i=0; i<lena; i++)
76             {
77                 if(color[i]) continue;
78                 if(s1[i]==','&&pre==',') continue;
79                 if(('a'<=s1[i]&&s1[i]<='z')||('0'<=s1[i]&&s1[i]<='9')) ans++;
80                 pre=s1[i];
81                 if(s1[i]==','&&ans==0) continue;
82                 if(!color[i]) printf("%c",s1[i]);
83             }
84             puts("");
85         }
86         else
87         {
88             s1[lena-1]='\0';
89             printf("%s",s1);
90             if(lenb!=2&&lena!=2)
91                 printf(",");
92             for(int i=1; i<lenb; i++)
93                 printf("%c",s2[i]);
94             puts("");
95         }
96     }
97     return 0;
98 }

 

 

 

posted @ 2012-11-26 17:53  Mr. Ant  阅读(310)  评论(0编辑  收藏  举报