HDOJ 2360 Word Ladder
BFS+路径保存即可。枚举所有单词作为“第一阶”,找只差一个字母的单词扩展节点,记录该单词前驱,当扩展的单词和第一个单词完全不同时结束BFS。枚举N次,每次碰到比原有情况更短的“阶梯”,就把路径保存下来。题目要求多结果时按字典序输出,只要在读入单词后,对所有单词按字典序排序就可以啦。
有一个比较重要的优化就是对所有单词进行预处理,把只相差一个字母的一对单词和完全不同的一对单词记录下来(我用邻接矩阵保存),然后在进行bfs。在枚举过程中,碰到有阶梯数刚好为l+1的情况,就可以直接结束枚举的过程了(不可能有比l+1更短的情况),由于数据量不大,手打快排和qsort()没太大区别。
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- #define OVER 70000
- int t,n,l;
- char word[101][MAX];
- int path[101],root[101];
- bool one[110][110],tol[110][110];
- bool hash[101],h[27];
- struct node{
- int num;
- int cost;
- };
- node now,next;
- node que[70001];
- int ql,qh;
- inline void QuickSort(int low, int high)
- {
- char key[MAX];
- int LOW,HIGH;
- if(low>high) return ;
- strcpy(key,word[low]);
- LOW=low;HIGH=high;
- while(low<high)
- {
- while(high>low)
- {
- if(strcmp(key,word[high])>0)
- {
- strcpy(word[low],word[high]);
- low++;break;
- }
- high--;
- }
- while(low<high)
- {
- if(strcmp(key,word[low])<0)
- {
- strcpy(word[high],word[low]);
- high--;break;
- }
- low++;
- }
- }
- strcpy(word[low],key);
- QuickSort(LOW,low-1);
- QuickSort(low+1,HIGH);
- }
- //inline int cmp(const void *a,const void *b)
- //{
- // char* c=(char *)a;
- // char* d=(char *)b;
- // return strcmp(c,d);
- //}
- inline int comp(const int &x,const int &y)
- {
- int c,flag;
- memset(h,0,sizeof(h));c=0;
- for(int i=0;i<l;i++)
- {
- flag=0;
- for(int j=0;j<l;j++)
- if(!h[j] && word[x][i]==word[y][j])
- {c++;h[j]=1;break;}
- }
- return c;
- }
- inline int bfs(const int &x)
- {
- ql=0;qh=0;
- now.num=x;now.cost=1;hash[x]=1;
- que[qh++]=now;if(qh>OVER) qh%=OVER;
- while(ql<qh)
- {
- now=que[ql++];if(ql>OVER) ql%=OVER;
- if(tol[now.num][x]==1) return now.cost;
- for(int i=0;i<n;i++)
- {
- if(hash[i]) continue;
- if(one[now.num][i]==1)
- {
- next.num=i;next.cost=now.cost+1;
- que[qh++]=next;if(qh>OVER) qh%=OVER;
- hash[i]=1;root[i]=now.num;
- }
- }
- }
- return 0;
- }
- int main()
- {
- int minpath,tem;
- scanf("%d",&t);
- while(t--)
- {
- scanf("%d %d",&n,&l);
- for(int i=0;i<n;i++)
- scanf("%s",word[i]);
- //qsort(word,n,sizeof(word[0]),cmp);
- QuickSort(0,n-1);
- memset(tol,0,sizeof(tol));
- memset(one,0,sizeof(one));
- for(int i=0;i<n-1;i++)
- for(int j=i+1;j<n;j++)
- {
- tem=comp(i,j);
- if(tem==l-1) {one[i][j]=1;one[j][i]=1;}
- if(tem==0) {tol[i][j]=1;tol[j][i]=1;}
- }
- minpath=200;
- for(int i=0;i<n && minpath!=l+1;i++)
- {
- memset(hash,0,sizeof(hash));
- memset(root,-1,sizeof(root));
- path[0]=bfs(i);
- if(path[0]>l && path[0]<minpath)
- {
- minpath=path[0];
- for(int j=1;j<=minpath;j++)
- {path[j]=now.num;now.num=root[now.num];}
- }
- }
- for(int i=minpath;i>0;i--)
- {
- if(i==1) printf("%sn",word[path[i]]);
- else printf("%s ",word[path[i]]);
- }
- }
- return 0;
- }
JstardictBB–>TeeDict
Posted by twcai in My BlackBerry on August 28, 2008
–2008-06-25 22:42
为我的BB 8100安装了JstardictBB,运行没有问题。但一直无法正确的添加从王网上下载的辞典,至今JstardictBB都是一个空壳,郁闷。同学的8100也遇到了同样的问题。最近比较忙,以后慢慢探索吧。
- -!
Update at 2008-6-27 : 现在JstardictBB已升级为TeeDict,安装以后设置为自动搜索词典就可以解决上面所说的问题了。如果再搜索词典的过程中,Black Berry不停得要求你选择是否让TeeDict程序访问本地的文件夹,那么只要在BB的安全选项里,打开防火墙就可以了,这样BB只会询问一次。
下载地址可以在作者的博客找到
http://oranbb.blogbus.com/logs/23611581.html
BB遇险
Posted by twcai in My BlackBerry on August 28, 2008
–2008-06-20 00:01
晚上在blackberry8100上安装了一个金山词霸,虽然已经有Jstardict了,但为了比较哪个字典更好用,我非常欠揍的把金山安装上了,完美运行。
顺便一提,我的BB8100是4.5的rom,我比较喜欢4.5的英文界面,因为感觉这个界面本来就没考虑到中文,待机画面下的中文字体非常
新分类 My BlackBerry
Posted by twcai in My BlackBerry on August 28, 2008
原来的空间上有两篇关于BlackBerry的日志浏览量还挺大的,是我关于自己使用BlackBeryy手机的一点心得和记录。
也拿到这边来充实一下。
第一篇日志
Posted by twcai in Some parts on August 28, 2008
弄了三个钟头,弄出了这点东西。发现要想随心所欲的修改css,必须要会php才行,不然永远都局限别人的框架里是一件很不爽的事情。
目前找到的这个模板我还是挺满意的,比较简洁,也不算难看,顺眼就好了,呵呵。以后主要都在这里更新啦。
另外发个代码测试一下coolcode插件:
- //Just a Hello World.
- #include <iostream>
- using namespace std;
- int main()
- {
- printf("%s\n","Hello World!");
- return 0;
- }//end for main
测试下第二段代码
- #include <iostream>
- using namespace std;
- int main()
- {
- printf("%s\n","Hello World!");
- return 0;
- }//end for main









