题目:http://poj.org/problem?id=3007

题意:按照图示的改变字符串,问有多少种。。字符串。。

思路:分几种排序的方法,,刚开始用map 超时(map效率不高啊。。),后来搜了一下题解,用二叉排序树。。。

先贴AC代码:

 #include <iostream>
#include <cstring>
#include <algorithm>
using namespace std; typedef struct tree
{
char str[];
struct tree *l,*r;
}tr;
tr head;
int check(char s[])
{
tr *q;
tr *p=&head;
while(p)
{
if(strcmp(p->str,s)>)
{
if(p->l)
p=p->l;
else
{
q=new tr;
strcpy(q->str,s);
q->l=NULL;
q->r=NULL;
p->l=q;
return ;
}
}
else if(strcmp(p->str,s)<)
{
if(p->r)
p=p->r;
else
{
q=new tr;
strcpy(q->str,s);
q->l=NULL;
q->r=NULL;
p->r=q;
return ;
}
}
else
return ;
}
return ;
}
/*void pre(tr *qqq)
{
if(qqq)
{
cout<<qqq->str<<endl;
pre(qqq->l);
pre(qqq->r);
}
}*/
int main()
{
int t,i,j,len,cou,sum;
char s[],s1[];
cin>>t;
while(t--)
{
cin>>s;
strcpy(head.str,s);
head.l=NULL; head.r=NULL;
sum=;
len=strlen(s);
for(i=; i<len; i++)
{
cou=;
for(j=; j<i; j++)
s1[cou++]=s[j];
for(j=len-; j>=i; j--)
s1[cou++]=s[j];
s1[cou]='\0';
if(check(s1))
sum++; cou=;
for(j=len-; j>=i; j--)
s1[cou++]=s[j];
for(j=; j<i; j++)
s1[cou++]=s[j];
s1[cou]='\0';
if(check(s))
sum++; cou=;
for(j=i-; j>=; j--)
s1[cou++]=s[j];
for(j=len-; j>=i; j--)
s1[cou++]=s[j];
s1[cou]='\0';
if(check(s1))
sum++; cou=;
for(j=len-; j>=i; j--)
s1[cou++]=s[j];
for(j=i-; j>=; j--)
s1[cou++]=s[j];
s1[cou]='\0';
if(check(s1))
sum++; cou=;
for(j=; j<i; j++)
s1[cou++]=s[j];
for(j=i; j<=len-; j++)
s1[cou++]=s[j];
s1[cou]='\0';
if(check(s1))
sum++; cou=;
for(j=i; j<=len-; j++)
s1[cou++]=s[j];
for(j=; j<i; j++)
s1[cou++]=s[j];
s1[cou]='\0';
if(check(s1))
sum++; cou=;
for(j=i-; j>=; j--)
s1[cou++]=s[j];
for(j=i; j<=len-; j++)
s1[cou++]=s[j];
s1[cou]='\0';
if(check(s1))
sum++; cou=;
for(j=i; j<=len-; j++)
s1[cou++]=s[j];
for(j=i-; j>=; j--)
s1[cou++]=s[j];
s1[cou]='\0';
if(check(s1))
sum++; cou=;
for(j=len-; j>=i; j--)
s1[cou++]=s[j];
for(j=; j<=i-; j++)
s1[cou++]=s[j];
s1[cou]='\0';
if(check(s1))
sum++;
}
cout<<sum<<endl;
//tr *qqq;
//qqq=&head;
//pre(qqq);
}
return ;
}

再贴一下map超时的。。

 #include <iostream>
#include <cstring>
#include <map>
#include <algorithm>
using namespace std; int main()
{
int t,i,j,len,cou,sum;
char s[],s1[];
int f;
cin>>t;
while(t--)
{
map<string,int>mp;
map<string,int>::iterator iter;
cin>>s;
sum=;
len=strlen(s);
for(i=; i<len; i++)
{
cou=;
for(j=; j<i; j++)
s1[cou++]=s[j];
for(j=len-; j>=i; j--)
s1[cou++]=s[j];
s1[cou]='\0';
f=;
iter=mp.begin();
while(iter!=mp.end())
{
if(iter->first==s1)
{
f=;
break;
}
iter++;
}
if(f==)
{
sum++;
mp[s1]=;
}
cou=;
for(j=len-; j>=i; j--)
s1[cou++]=s[j];
for(j=; j<i; j++)
s1[cou++]=s[j];
s1[cou]='\0';
f=;
iter=mp.begin();
while(iter!=mp.end())
{
if(iter->first==s1)
{
f=;
break;
}
iter++;
}
if(f==)
{
sum++;
mp[s1]=;
} cou=;
for(j=i-; j>=; j--)
s1[cou++]=s[j];
for(j=len-; j>=i; j--)
s1[cou++]=s[j];
s1[cou]='\0';
f=;
iter=mp.begin();
while(iter!=mp.end())
{
if(iter->first==s1)
{
f=;
break;
}
iter++;
}
if(f==)
{
sum++;
mp[s1]=;
} cou=;
for(j=len-; j>=i; j--)
s1[cou++]=s[j];
for(j=i-; j>=; j--)
s1[cou++]=s[j];
s1[cou]='\0';
f=;
iter=mp.begin();
while(iter!=mp.end())
{
if(iter->first==s1)
{
f=;
break;
}
iter++;
}
if(f==)
{
sum++;
mp[s1]=;
} cou=;
for(j=; j<i; j++)
s1[cou++]=s[j];
for(j=i; j<=len-; j++)
s1[cou++]=s[j];
s1[cou]='\0';
f=;
iter=mp.begin();
while(iter!=mp.end())
{
if(iter->first==s1)
{
f=;
break;
}
iter++;
}
if(f==)
{
sum++;
mp[s1]=;
} cou=;
for(j=i; j<=len-; j++)
s1[cou++]=s[j];
for(j=; j<i; j++)
s1[cou++]=s[j];
s1[cou]='\0';
f=;
iter=mp.begin();
while(iter!=mp.end())
{
if(iter->first==s1)
{
f=;
break;
}
iter++;
}
if(f==)
{
sum++;
mp[s1]=;
}
cou=;
for(j=i-; j>=; j--)
s1[cou++]=s[j];
for(j=i; j<=len-; j++)
s1[cou++]=s[j];
s1[cou]='\0';
f=;
iter=mp.begin();
while(iter!=mp.end())
{
if(iter->first==s1)
{
f=;
break;
}
iter++;
}
if(f==)
{
sum++;
mp[s1]=;
} cou=;
for(j=i; j<=len-; j++)
s1[cou++]=s[j];
for(j=i-; j>=; j--)
s1[cou++]=s[j];
s1[cou]='\0';
f=;
iter=mp.begin();
while(iter!=mp.end())
{
if(iter->first==s1)
{
f=;
break;
}
iter++;
}
if(f==)
{
sum++;
mp[s1]=;
}
}
cout<<sum<<endl;
}
return ;
}

poj 3007 Organize Your Train part II(二叉排序树)的更多相关文章

  1. POJ 3007 Organize Your Train part II

    题意: 如上图所示,将一个字符串进行分割,反转等操作后不同字符串的个数: 例如字符串abba:可以按三种比例分割:1:3:2:2:3:1 部分反转可以得到如下所有的字符串: 去掉重复可以得到六个不同的 ...

  2. POJ 3007 Organize Your Train part II (字典树 静态)

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6478   Acce ...

  3. POJ 3007 Organize Your Train part II(哈希链地址法)

    http://poj.org/problem?id=3007 题意 :给你一个字符串,让你无论从什么地方分割,把这个字符串分成两部分s1和s2,然后再求出s3和s4,让你进行组合,看能出来多少种不同的 ...

  4. poj 3007 Organize Your Train part II(静态字典树哈希)

    Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6700 Accepted: 1922 Description RJ Freigh ...

  5. POJ 3007:Organize Your Train part II

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7561   Acce ...

  6. Organize Your Train part II 字典树(此题专卡STL)

    Organize Your Train part II Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Acce ...

  7. poj Organize Your Train part II

    http://poj.org/problem?id=3007 #include<cstdio> #include<algorithm> #include<cstring& ...

  8. Organize Your Train part II(hash)

    http://poj.org/problem?id=3007 第一次用STL做的,TLE了,自己构造字符串哈希函数才可以.. TLE代码: #include <cstdio> #inclu ...

  9. Organize Your Train part II-POJ3007模拟

    Organize Your Train part II Time Limit: 1000MS Memory Limit: 65536K Description RJ Freight, a Japane ...

随机推荐

  1. JAVA对象是如何占用内存的

      本文使用的是32位的JVM ,jdk1.6.本文基本是翻译的,加上了一些自己的理解,原文见文章底下链接.     在本文中,我们讨论如何计算或者估计一个JAVA对象占多少内存空间.(注意,使用 C ...

  2. discuz random函数

    在研究邮箱非必填的过程中发现了个比较好用的random函数,在function_core.php中找到声明: function random($length, $numeric = 0) { $see ...

  3. php调试利器 -- xdebug

    之前整理在印象笔记中,现在搬出来.分本地调试和远程调试.本文亲测通过并截图,有问题欢迎留言探讨. (参考网上多位前辈的博客,整理在笔记里忘记保存原文链接,这里无法贴出链接,望见谅)   # 痛处 一般 ...

  4. V9任何页面GET调用内容分页的说明

    如标题,很多人想要在网站首页或其他的页面实现分页效果,说明如下: 一般特殊页面实现分页是通过GET语句的(论坛很多牛人用修改PHPCMS系统函数来实现,个人不推荐,因为你改了系统文件,不利于官方下一步 ...

  5. VB6-操作数据库

    平常搞数据库操作多了就想把经常用的内容放在一起,我也懒,在一本书里的工程例子挑了一个bas,修修改改,凑合这用吧. Public strCnn As String '数据库连接字符串 Public A ...

  6. IP HELPER GetAdaptersAddresses 函数

    自己做的一些笔记,XP以及以后的系统使用: MSDN 函数:http://msdn.microsoft.com/en-US/library/windows/desktop/aa365915(v=vs. ...

  7. ubuntu 安装dell无线网卡2

    以下转自:http://blog.sina.com.cn/s/blog_73b6331101016haq.html ubuntu 12.04 bcm43xx无线网卡安装记录 (2012-07-01 0 ...

  8. Thinkcmf 在新浪云上的部署问题

    最近要开发一个社团主页,于是想到了CMF内容管理系统的,但是直接在自己的服务器测试成本太高,于是选择了在新浪云上进行部署测试. 但是在安装Thinkcmf的过程中产生了一些技术性的问题.但最后终于在自 ...

  9. Oracle中对象权限与系统权限revoke

    实验: 1.以sys登陆,创建用户test1, test2: 2.授予test1系统权限(带admin option)-connect,授予test1对象权限(带grant option)-selec ...

  10. 解决eclipse复制粘贴js代码卡死的问题

    鸣谢:http://blog.csdn.net/zhangzikui/article/details/24805935 ---------------------------------------- ...