STL 【map】【string】小小结

START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i’m fiwo riwosf.
i fiiwj fnnvk!
END
Sample Output
hello, i’m from mars.
i like earth!
题目大意 将一个单词 转换为 另一单词
代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define LL long long
#define M  10000
#define inf 0x3f3f3f3f
#define mod 100009
using namespace std;
map<string,string>mp;
char tmp[3000+100];
int main()
{
    string a,b; //  
    cin>>a;  // start   cin 遇到空格断,且可以自动过滤回车 
    while(cin>>a&&a!="END") 
    {
    cin>>b;  // string 只能够用 cin 来获取 
    mp[b]=a;
     } 
     cin>>a;  // 吃start
    getchar(); //  吃回车 
//只有在输入数字 和 用gets()之间是要用getchar吃空行的,对于连续多个用gets输入,,会自动吃空行的
     while(1)
     {
        gets(tmp); // 这里就是多次连续输入 gets ,但是却不用吃空行
        if(strcmp(tmp,"END")==0) break;
        int i,len;
        len=strlen(tmp);
        b=""; // 清空string b
        for(i=0;i<len;i++)
        {
             if(!(tmp[i]>='a' && tmp[i]<='z'))//非小写字母
             {
                if(mp[b]!="") //存在这个单词
                     cout<<mp[b];
               else  cout<<b;
                 b="";
                 cout<<tmp[i];
              }
             else //小写字母
               b+=tmp[i];   // 这个很方便 
       }
         cout<<endl;  // 换行
     }
    return 0;
}

Sample Input
1
5
apple shandong 3
pineapple guangdong 1
sugarcane guangdong 1
pineapple guangdong 3
pineapple guangdong 1
Sample Output
guangdong
|—-pineapple(5)
|—-sugarcane(1)
shandong
|—-apple(3)

代码

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define LL long long
#define M  10000
#define inf 0x3f3f3f3f
#define mod 100009
using namespace std;
map<string, map < string , int > > mp;  //  嵌套  中间一定要有一些 空格 
map< string , map < string , int > >::iterator it1;
map<string,int >::iterator it2;  //map 中可以将 原像 和 像都 输出出来 it2.first  it2.second  
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int m;
        scanf("%d",&m);
        mp.clear();
        while(m--)
        {

            string a,b;int num;
            cin>>a>>b>>num;
            mp[b][a]+=num;
        }  // map  中存储时候,是按照 原像 的字典序存储的, 
        for(it1=mp.begin();it1!=mp.end();it1++)
        {
            cout<<it1->first<<endl;
            for(it2=it1->second.begin();it2!=it1->second.end();it2++)
            {
                cout<<"   |----";  // C++中 endl 代表了换行 
                cout<<it2->first<<"("<<it2->second<<")"<<endl;
            }
        }
        if(t) putchar('\n'); 
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值