字符串模拟(g2g c u l8r)

在这里插入图片描述

传送门:g2g c u l8r

我写的代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <set>
#include <map>
#include <sstream>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define f(i,a,n) for(int i=a;i<n;++i)
#define ff(i,a,n) for(int i=a;i<=n;++i)
using namespace std;
typedef long long ll;
typedef double dbl;
typedef pair<int, int> pi;
const int INF = 0x3f3f3f3f;
inline ll in() { char ch = getchar();ll x = 0, f = 1;while (ch<'0' || ch>'9') { if (ch == '-')f = -1;ch = getchar(); }while (ch >= '0'&&ch <= '9') { x = x * 10 + ch - '0';ch = getchar(); }return x * f; }
map<string,string> mp;
void solve(){
    int n,t;
    cin>>n;
    string ss,s;
    while(n--){
        cin>>s;
        getchar();
        getline(cin,ss);
        mp[s]=ss;
    }
    cin>>t;
    getchar();
    while (t--){
        string s1,s2;
        getline(cin,s1);
        for (int i = 0; i<s1.length(); ++i) {
            if(s1[i]==' '){
                if(mp.find(s2)!=mp.end()){
                    cout<<mp[s2]<<" ";
                }
                else cout<<s2<<" ";
                s2="";
            }
            else if(i==s1.length()-1){
                s2+=s1[i];
                if(mp.find(s2)!=mp.end()){
                    cout<<mp[s2]<<" ";
                }
                else cout<<s2<<" ";
                s2="";
            }
            else s2+=s1[i];
        }
        cout<<endl;
    }

}
int main(){
    int T=1;
    while(T--) {
        solve();
    }
    return 0;
}

别人家的代码QAQ:

#include<iostream>
#include<map>
using namespace std;
string a,b,c;
map<string,string>mp;
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>a;
        getchar();
        getline(cin,b);
        mp[a]=b;
    }
    int m;
    cin>>m;
    while(m){
        cin>>c;
        if(mp.count(c))cout<<mp[c];
        else cout<<c;
        char ch=getchar();
        if(ch == '\n')
            m--;
        cout<<ch;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值