HDOJ1075解题报告【STL】

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1075

题目概述:

  首先给你一些英语单词以及它们在火星文中对应的单词,然后给你一些用火星文写的字符串,请你翻译。

大致思路:

  其实就是个很简单的字符串替换,麻烦就麻烦在如何存储字符串之间的对应关系。

  直接上STL,用map可以很好地解决这一问题,剩下的就只有一些细节要处理了。

复杂度分析:

  取决于STL中map的插入,查找的复杂度。

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <vector>
 6 #include <ctime>
 7 #include <map>
 8 #include <queue>
 9 #include <cstring>
10 #include <algorithm>
11 using namespace std;
12 
13 #define sacnf scanf
14 #define maxn 1010
15 #define inf 1061109567
16 #define Eps 0.001
17 #define PI 3.1415927
18 #define mod 1000000007
19 void Swap(int &a,int &b) {int t=a;a=b;b=t;}
20 int Abs(int x) {return (x<0)?-x:x;}
21 typedef long long ll;
22 
23 map<string,string> dict;
24 map<string,int> vis;
25 
26 char s[3010];
27 
28 int main()
29 {
30     //freopen("data.in","r",stdin);
31     //freopen("data.out","w",stdout);
32     //clock_t st=clock();
33     string a,b;char c;
34     cin>>a;
35     while(cin>>a)
36     {
37         if(a[0]=='E') break;
38         scanf("%c",&c);
39         cin>>b;dict[b]=a;vis[b]=1;
40     }
41     cin>>a;scanf("%c",&c);
42     while(1)
43     {
44         cin.getline(s,3001,'
');
45         if(s[0]=='E') break;
46         int len=strlen(s);
47         string word;
48         for(int i=0;i<len;i++)
49         {
50             if(s[i]>='a'&&s[i]<='z')
51             {
52                 word+=s[i];
53             }
54             else
55             {
56                 if(vis[word]==1)
57                 {
58                     cout<<dict[word];
59                 }
60                 else cout<<word;
61                 printf("%c",s[i]);
62                 word.clear();
63             }
64         }
65         if(!word.empty())
66         {
67             if(vis[word]==1)
68             {
69                 cout<<dict[word];
70             }
71             else cout<<word;
72         }
73         printf("
");
74     }
75     //clock_t ed=clock();
76     //printf("

Time Used : %.5lf Ms.
",(double)(ed-st)/CLOCKS_PER_SEC);
77     return 0;
78 }
原文地址:https://www.cnblogs.com/CtrlKismet/p/6241246.html