uva 12504

原題

簡單題,找不同, 用map存儲dictionary就行了

要注意的是value雖然是數字, 但不定長, 因此也需要用string類型來存儲

這題寫得有點醜.. 僅供娛樂

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <list>
#include <cassert>
#include <iomanip>

using namespace std;
const int MAXN = 100+100;
typedef long long LL;

/*
uva 12504


*/

map<string,string> dic;
map<string,bool>   mark;
vector<string> newk, removed, changed;
char input[MAXN];

void initial(){
	dic.clear();
	mark.clear();
	memset(input,0,sizeof(input));
	newk.clear();
	removed.clear();
	changed.clear();
	return ;
}

void output(vector<string> & vec,char ch){
	if( vec.empty() ) return ;
	sort(vec.begin(),vec.end());
	cout << ch << vec[0];
	for(int i=1; i<vec.size(); i++){
		cout << "," << vec[i];
	}
	cout << endl;
}

int main(){
	int T;
//	freopen("input2.txt","r",stdin);
	scanf("%d ",&T);
	while( T-- ){
		initial();
		gets(input);
		char * ptr = input+1;
		int len = strlen(input);
		string key;
		
		for(int i=1; i<len; i++){
			if( input[i]==':' ){
				input[i] = '\0';
				if( strlen(ptr) ) key = ptr;
				ptr = (input+i+1);
			}else if( input[i]==',' || input[i]=='}'){
				input[i] = '\0';
				if( strlen(ptr) ){
					dic[key] = string(ptr);
					mark[key] = true;
				}
				ptr = (input+i+1);
			}
		}
		
		gets(input);
		len = strlen(input); 
		ptr = input+1;
		for(int i=1; i<len; i++){
			if( input[i]==':' ){
				input[i] = '\0';
				if( strlen(ptr) ) key = ptr;
				ptr = (input+i+1);
			}else if( input[i]==',' || input[i]=='}'){
				input[i] = '\0';
				
				if( !mark[key] && strlen(ptr) ){
					newk.push_back(key);
				}else if( strlen(ptr) ){
					mark[key] = false;				// 把1有2也有的再次標記爲false, 以便後面檢查removed 
					string value = ptr;
					if( dic[key]!=value ){
						changed.push_back(key);
					}
				}
				ptr = (input+i+1);
			}
		}
		map<string,bool>::iterator it = mark.begin();
		while( it!=mark.end() ){
			if( it->second==true ){
				removed.push_back(it->first);
			}
			it++;
		}
		if( newk.empty() && removed.empty() && changed.empty() ){
			cout << "No changes" << endl;
		}else{
			output(newk,'+');
			output(removed,'-');
			output(changed,'*');
		}
		cout << endl;
	}
	
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章