编程之美a题小数据 学习用sstream 处理字符串

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//typedef long long LL;
//typedef __int64 LL;
//typedef long double DB;
//typedef unisigned __int64 LL;
//typedef unsigned long long ULL;
#define EPS  1e-8
#define MAXN 1600
#define INF  0x3f3f3f3f
#define PI   acos(-1.0)
//#define MOD  99991
//#define MOD  99990001
//#define MOD  1000000007
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define max3(a,b,c) (max(max(a,b),c))
#define min3(a,b,c) (min(min(a,b),c))
#define mabs(a) ((a<0)?(-a):a)
#define L(t) (t << 1)  //Left son t*2
#define R(t) (t << 1 | 1) //Right son t*2+1
#define Mid(a,b) ((a+b)>>1) //Get Mid
#define lowbit(a) (a&-a) //Get Lowbit
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a*b/gcd(a,b);}
struct word
{
    string ori,res;
}w[105];
int main()
{
//  freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
	int T;
	cin>>T;
	for(int i = 1 ; i <= T ; i++)
	{
		int a,b,cnt=0;
		cin>>a>>b;
		while(b--)
		{
		    string a1,b1;
		    cin>>a1>>b1;
		    w[cnt].ori = a1;
            w[cnt].res = b1;
            cnt++;
		}
		string xx,sub[105];
		getchar();
		getline(cin,xx);
		stringstream ss(xx);
		int j=0;
		while(ss>>sub[j++]){}
		while(--a)
		{
            for(int k=0 ; k<j ; k++)
            {
                for(int l=0 ; l<cnt; l++ )
                {
                    if(sub[k] == w[l].ori)
                    {
                        sub[k] = w[l].res;
                        break;
                    }
                }
            }
		}
		cout<<"Case #"<<i<<": ";
		for(int k=0 ; k<j ; k++)
            cout<<sub[k]<<" ";
		cout<<endl;
	}
	return 0;
}



最好的方法我觉得应该是 hash+置换 估计了一下大概复杂度也就O(m^2)


 但是自己太懒实在不愿写就直接写了一个暴力A掉


以前一直不会用sstream...今天查了一下处理字符串确实方便...再也不用写substring这种又长又恶心的东西了

 

posted @ 2013-04-06 17:16  xinyuyuanm  阅读(163)  评论(0编辑  收藏  举报