一开始我也想用map 但是处理不好其他字符。。
看了题解 多多学习! 很巧妙 就是粗暴的一个字符一个字符的来 分为小写字母和非小写字母两个部分 一但单词结束的时候就开始判断。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<bits/stdc++.h> using namespace std; int main() { string a,b; map<string ,string >ma; cin>>a; while(cin>>a&&a!="END") { cin>>b;ma[b]=a; } cin>>a; char s[3500];getchar(); while(gets(s)) { if(!strcmp(s,"END"))break; int n=strlen(s); a=""; for(int i=0;i<n;i++) { if(islower(s[i]))a+=s[i]; else { if(ma.find(a)!=ma.end()) cout<<ma[a]; else cout<<a; cout<<s[i]; a=""; } } cout<<endl; } }
字典树写法
注意malloc 和初始化 字符串赋值用strcpy 不申请内存根本无法使用
因为 gets 和getchar 的问题检查了半小时 注意!!!
gets会吸收 给忘记了。。。。
有关字典树的指针写法规范一下
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream> #include <cstdio> #include <cstring> #include<malloc.h> using namespace std; struct node { char *val; node *next[26]; int flag; node() { for(int i=0;i<26;i++) { next[i]=NULL; } flag=0; } }; node *p,*root=new node(); void change(char *s,char *v) { p=root; for(int i=0;s[i]!='