HDU 1075 What Are You Talking About
- 题意:将火星文翻译为英文。输入的单词如果有对应的英文则输出,如果没有就输出原火星文,空格和标点符号按原文输出。
- 思路:输入的整个字符串,我们跑s[ 0 ]->s[ len ]。如果是字母,就加在tmp里;如果不是字母,就判断tmp里的单词是不是有对应的英文,如果有就输出英文,如果没有就输出原文,然后把空格或者标点输出。【注意】字符串可能以单词结尾,所以我们跑到s[ len ]才结束。
STL_map:
mp[ 火星文 ] = 英文
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define INF 0x3f3f3f3f
#define P(x) x>0 ? x : 0
#define MID l + r >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define lowbit(x) x & (-x)
using namespace std;
typedef long long ll;
typedef vector<int>:: iterator VITer;
const int maxN=3e3+5;
map<string,string>mp;
map <string , string> :: iterator it;
int main()
{
char flag[10];
scanf("%s", flag);
char English[15],Mars[15];
while(scanf("%s", English) && strcmp(English, "END") != 0)
{
scanf("%s", Mars);
mp[Mars] = English;
}
scanf("%s", flag);
getchar();
char s[maxN];
while(gets(s) && strcmp(s, "END") != 0)
{
char tmp[15]= {'\0'};
int cnt = 0;
int len = strlen(s);
for(int i = 0; i <= len ; i++)
{
if( isalpha(s[i]) )
tmp[cnt++] = s[i];
else
{
if(strlen(tmp))
{
it = mp.find(tmp);
if(it != mp.end())
cout<<it->second;
else
printf("%s", tmp);
}
if(i < len)
{
putchar(s[i]);
memset(tmp, 0, sizeof(tmp));
cnt = 0;
}
}
}
putchar('\n');
}
return 0;
}
字典树
将火星文用树存起,然后单词结尾的 rt 用二维字符数组存对应的英文。Search的时候必须是找到了单词结尾才算是存在该单词。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <list>
#include <map>
#define INF 0x3f3f3f3f
#define MID l + r >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define lowbit(x) x & (-x)
using namespace std;
typedef long long ll;
typedef vector<int>:: iterator VITer;
const int maxN=5e5+5;
char val[maxN][15];
int tire[maxN][26], tot, rt;
char Mars[15], English[15];
void init()
{
memset(tire, 0 , sizeof(tire));
memset(val, 0 , sizeof(val));
tot = 0;
}
void Insert()
{
rt=0;
int len = strlen(Mars);
for(int i = 0; i < len ; i++)
{
int id = Mars[i] - 'a';
if( ! tire[rt][id] ) tire[rt][id] = ++ tot;
rt = tire[rt][id];
}
strcpy(val[rt], English);
}
int Search(char *str)
{
int len=strlen(str);
rt = 0;
for(int i = 0; i < len ; i++)
{
int id=str[i]- 'a';
if( ! tire[rt][id] ) return 0;
rt = tire[rt][id];
}
if(strcmp(val[rt],"\0") == 0)
return 0;
return rt;
}
int main()
{
init();
char flag[10]; scanf("%s", flag);
while(scanf("%s", English) && strcmp(English, "END") != 0)
{
scanf("%s", Mars);
Insert();
}
scanf("%s", flag);
getchar();
char s[maxN];
while(gets(s) && strcmp(s, "END") != 0)
{
int len = strlen(s);
char tmp[15] = {'\0'}; int cnt = 0;
for(int i = 0; i <= len ; i++)
{
if( isalpha(s[i]) )
tmp[cnt ++] = s[i];
else
{
if(cnt)
{
if(int root = Search(tmp))
printf("%s", val[root]);
else
printf("%s", tmp);
}
if(i < len)
{
putchar(s[i]);
memset(tmp, 0 , sizeof(tmp));
cnt = 0;
}
}
}
putchar('\n');
}
return 0;
}
二刷字典树做法
PE了一发,因为gets会读回车!!!!
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxN = 3200000 + 7;
char Eng[20], Mar[20];
int tire[maxN][26], tot;
char ans[maxN][20];
void Clear(int rt)
{
for(int i = 0; i < 26; i ++ ) tire[rt][i] = 0;
}
void Insert(char *s)
{
int rt = 0;
int len = strlen(s);
for(int i = 0; i < len; i ++ )
{
int id = s[i] - 'a';
if(!tire[rt][id]) { tire[rt][id] = ++ tot; Clear(tot); }
rt = tire[rt][id];
}
strcpy(ans[rt], Eng);
}
int Search(char *s)
{
int rt = 0;
int len = strlen(s);
for(int i = 0; i < len; i ++ )
{
int id = s[i] - 'a';
if(!tire[rt][id]) return -1;
rt = tire[rt][id];
}
if(strlen(ans[rt]) == 0) return -1;
return rt;
}
int main()
{
Clear(0); tot = 0;
while(~scanf("%s", Eng) && strcmp(Eng, "END") != 0)
{
if(strcmp(Eng, "START") == 0) continue;
scanf("%s", Mar);
Insert(Mar);
}
getchar();
char q[3005];
while(gets(q) && strcmp(q, "END") != 0)
{
if(strcmp(q, "START") == 0) continue;
int len = strlen(q);
char tmp[20]; int cnt = 0;
for(int i = 0; i < len; i ++ )
{
if(isalpha(q[i]))
tmp[cnt ++ ] = q[i];
else
{
tmp[cnt] = '\0';
if(cnt)
{
int rt = Search(tmp);
if(rt != -1)
printf("%s", ans[rt]);
else
printf("%s", tmp);
memset(tmp, 0, sizeof(tmp));
cnt = 0;
}
putchar(q[i]);
}
}
putchar('\n');
}
return 0;
}