【UVA】12504 - Updating a Dictionary(map,string,vector模拟)

一般的模拟题,一开始WA,可能是用string的容器放char,改成string就过了

14073581 12504 Updating a Dictionary Accepted C++ 0.032 2014-08-21 07:12:19

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<string>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF (1 << 10)
#define esp 1e-9
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===========================================*/
#define MAXD 100 + 10
map<string,string>pre;
map<string,string>now;
vector<string>add;
vector<string>cost;
vector<string>change;
vector<string>_str1;
vector<string>_str2;
void init(){
    pre.clear();
    now.clear();
    add.clear();
    cost.clear();
    change.clear();
    _str1.clear();
    _str2.clear();
}
void solve1(string str){
    int L = str.size();
    string key = "",value = "";
    int ok = 0;
    for(int i = 1 ; i < L - 1; i++){
         if(str[i] == ':'){
             _str1.push_back(key);
             ok = 1;
         }
         else if(str[i] == ','){
             pre[key] = value;
             value = "";
             key = "";
             ok = 0;
         }
         else if(!ok)
            key += str[i];
         else
            value += str[i];
    }
    pre[key] = value;
}
void solve2(string str){
    int L = str.size();
    string key = "",value="";
    int ok = 0;
    for(int i = 1 ; i < L - 1; i++){
         if(str[i] == ':'){
             _str2.push_back(key);
             ok = 1;
         }
         else if(str[i] == ','){
             now[key] = value;
             value = "";
             key = "";
             ok = 0;
         }
         else if(!ok)
            key += str[i];
         else
            value += str[i];
    }
    now[key] = value;
}
void solve(){
    for(int i = 0 ; i < _str2.size() ; i++){
        string str = _str2[i];  /*在新字典里面找*/
        //cout << str <<" " <<  now[str] << endl;
        if(!pre.count(str))    /*旧字典没有这个*/
            add.push_back(str);
        else if(pre[str] != now[str])
            change.push_back(str);
    }
    for(int i = 0 ; i < _str1.size() ; i++){
        string str = _str1[i];  /*在旧字典里面找*/
        //cout << str <<" " << pre[str] << endl;
        if(!now.count(str))     /*新字典没有这个*/
            cost.push_back(str);
    }
}
int main(){
    int T;
    cin >> T;
    while(T--){
        init();
        string str;
        cin >> str;
        solve1(str);
        cin >> str;
        solve2(str);
        solve();
        if(!add.size() && !cost.size() && !change.size())
            cout << "No changes" << endl;
        else{
            if(add.size()){
                printf("+");
                sort(add.begin(),add.end());
                for(int i = 0 ; i < add.size() ; i++){
                    cout << add[i];
                    if(i < add.size() - 1)
                    cout << ",";
                    else
                    cout << endl;
                }
            }
            if(cost.size()){
                printf("-");
                sort(cost.begin(),cost.end());
                for(int i = 0 ; i < cost.size() ; i++){
                    cout << cost[i];
                    if(i < cost.size() - 1)
                    cout << ",";
                    else
                    cout << endl;
                }
            }
            if(change.size()){
                printf("*");
                sort(change.begin(),change.end());
                for(int i = 0 ; i < change.size() ; i++){
                    cout << change[i];
                    if(i < change.size() - 1)
                    cout << ",";
                    else
                    cout << endl;
                }
            }
        }
        cout << endl;
    }
    return 0;
}

【UVA】12504 - Updating a Dictionary(map,string,vector模拟)

时间: 2024-07-29 16:56:58

【UVA】12504 - Updating a Dictionary(map,string,vector模拟)的相关文章

UVa 12504 Updating a Dictionary(更新字典)

题意  比较两个字典  按字典序输出所有添加 删除 修改的项   如果没有任何更新  输出  No changes STL map的应用  对比两个字典  注意开始字符串的处理和字典可以为空 #include<bits/stdc++.h> using namespace std; map<string, string> d[2]; map<string, string>::iterator it; const int N = 105; string s, a, b, t

UVA 12504 Updating a Dictionary

题目链接:https://vjudge.net/problem/UVA-12504 题目翻译摘自<算法禁赛入门经典> 题目大意 在本题中,字典是若干键值对,其中键为小写字母组成的字符串,值为没有前导零或正号的非负整数(-4,03 和 +77 都是非法的,注意该整数可以很大).输入一个旧字典和一个新字典,计算二者的变化.输入的两个字典中键都是唯一的,但是排列顺序任意. 输入包含两行,各包含不超过100个字符,即旧字典和新字典.输出格式如下: 如果至少有一个新增键,打印一个“+”号,然后是所有新增

【UVA】12504 Updating a Dictionary(STL)

题目 题目 ? ? 分析 第一次用stringstream,真TMD的好用 ? ? 代码 #include <bits/stdc++.h> using namespace std; int main() { int n; cin>>n; getchar();//回车 while(n--) { string s1,s2; getline(cin,s1); getline(cin,s2); for(int i=0;i<s1.length();i++) if(!isalpha(s1

Uva 511 Updating a Dictionary

大致题意:用{ key:value, key:value, key:value }的形式表示一个字典key表示建,在一个字典内没有重复,value则可能重复 题目输入两个字典,如{a:3,b:4,c:10,f:16}  {a:3,c:5,d:10,ee:4} 于新字典相比,找出旧字典的不同,并且输出用相应格式,增加的用'+',如 +d,ee 减少的用'-',如 -b,f value变化的用*,如 *c 没有不同则输出"No changes"" 如果使用java,则可以用Tre

UVA 230 Borrowers(vector和map的应用,模拟)

1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 #include<cstring> 6 #include<string> 7 #include<iostream> 8 using namespace std; 9 typedef long long ll; 10 /** 11 题意:SHELVE

uva 11991 Easy Problem from Rujia Liu? vector+map

水题 学习一下数据的存储方法. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<stack> 5 #include<queue> 6 #include<vector> 7 #include<map> 8 using namespace std; 9 int n,m; 10 map<int,vector<int>

CSU 1113 Updating a Dictionary(map容器应用)

题目链接:http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1113 解题报告:输入两个字符串,第一个是原来的字典,第二个是新字典,字典中的元素的格式为一个关键字对应一个值,输入格式如下: {a:3,b:4,c:10,f:6} {a:3,c:5,d:10,ee:4}冒号前面的表示关键字,冒号后面的数字表示值,关键字由小写字母组成.现在让你判断,如果新的字典相对于原来的字典有新增的关键字以以下格式输出 :+key1,key2,....如果新的字典相对

Uva 10815-Andy&#39;s First Dictionary(串)

Problem B: Andy's First Dictionary Time limit: 3 seconds Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all

【UVA】230 - Borrowers(map模拟)

利用map<string,int>判断一本书的状态,0代表借出去了,1代表在书架,2代表借出去换回来但是还未放回书架 设计一些字符串的处理问题,用一些字符串搜索函数比如 strstr , strchar等等 14072706 230 Borrowers Accepted C++ 0.015 2014-08-21 02:59:27 AC代码: #include<cstdio> #include<cstring> #include<iostream> #incl