POJ1635 Subway tree systems

用CodeBlock写题,一个VS党深受折磨,文本编辑器一般般,编译错误的提示死活看不懂

好吧,毕竟是开源的

此题题意:

给出两串含有‘1’和‘0’的字符串,0表示向下搜索,1表示回溯,这样深搜一颗树
深搜完之后问这两棵树是不是同一棵树,因为树的结点顺序不同,所以可以导致树深搜的序列也不同


用最小表示法表示一颗子树

比如样例中的

0010011101001011
有3子树

00100111 01 001011

对子树00100111,有2子树01 0011,对子树0011,有1子树01

(01)(001011)(00100111)

对于所有的子树以最小表示法表示,字典序排序,判断最后的字符串是否相同

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool cmp(string a,string b)
{
    return a.compare(b)>0;
}
string dfs(string t)
{
    string tmp;
    vector<string>m;//所有的子串
    int i=0;
    int l=0;
    int counter0=0;
    int counter1=0;
    for(;i<t.size();i++)
    {
        if(t[i]=='0')
        {
            counter0++;
        }
        else
        {
            counter1++;
        }
        if(counter0==counter1)
        {
			if(counter0==1)
			{
				tmp=t.substr(l,i-l+1);
			}
			else
			{
            tmp=t.substr(l+1,i-l);
            tmp=dfs(tmp);
            tmp=t[l]+tmp+t[i];
			}
			m.push_back(tmp);
            l=i+1;
        }
    }
    sort(m.begin(),m.end(),cmp);
    tmp.clear();
    for(i=0;i<m.size();i++)
    {
        tmp+=m[i];
    }
    return tmp;
}
int main()
{
    int total;
	cin>>total;
    while(total--)
    {
        string a,b;
        cin>>a>>b;
        a=dfs(a);
        b=dfs(b);
        if(a==b)
        {
            cout<<"same"<<endl;
        }
        else
        {
            cout<<"different"<<endl;
        }
    }
    return 0;
}



你可能感兴趣的:(哈希,最小表示法)