题目:https://acm.hdu.edu.cn/showproblem.php?pid=1075
写的时候那个cin和getline混合用会多输出一个空行。。。报了两次格式错误。。这里卡了10几分钟。。
AC代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
using namespace std;
const int SIZE = 26;
typedef struct Tree{
string s;
struct Tree *next[SIZE];
bool exist;
Tree(){
s = "";
memset(next,0,sizeof(next));
exist = false;
}
}Tire;
Tire *T = new Tire();
void insert(Tire *&T,string &e,string &s){
Tire *t = T;
int len = s.size();
for(int i = 0;i<len;i++){
if(t->next[s[i] - 'a'] == NULL)
t->next[s[i]-'a'] = new Tire();
t = t->next[s[i]-'a'];
}
t->s = e;
t->exist = true;
}
string search(Tire *T,string &s){
int len = s.size();
Tire *t = T;
for(int i = 0;i<len; i++){
if(t->next[s[i]-'a'] == NULL)
return "";
t = t->next[s[i]-'a'];
}
return t->s;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
string e,s;
while(1){
getline(cin,e);
if(e == "START"){
continue;
}
if(e == "END"){
break;
}
for(int i = 0;i<e.size();i++){
if(e[i] == ' '){
s = e.substr(i+1,e.size()-i-1);
e = e.substr(0,i);
break;
}
}
insert(T,e,s);
}
while(1){
getline(cin,s);
if(s == "START") continue;
if(s == "END") break;
string ans = "";
int len = s.size();
string t;
for(int i = 0;i<len;i++){
if(s[i] >= 'a' && s[i] <= 'z')
t += s[i];
else{
string trans = search(T,t);
if(trans.size() == 0) ans += t;
else ans += trans;
ans += s[i];
t.clear();
}
}
cout << ans << endl;
}
system("pause");
return 0;
}