UVaOJ 112道题目-字符串
2014-11-18 15:49
260 查看
1、110301/10082 WERTYU (WERTYU 键盘)
View Code
#include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<ctype.h> #include<queue> using namespace std; char word[26000][20]; int h[26000]; int nxt[1000000]; int node[1000000]; int vis[26000]; int dis[26000]; int cnt=0; int q[26000]; int pre[26000]; int route[26000]; int GetInd(char src[]) { for(int i=0;i<cnt;i++) { if(strcmp(src,word[i])==0)return i; } return -1; } int _strcmp(char src[],char dest[],int len) { int tot=0; for(int i=0;i<len;i++) if(src[i]!=dest[i])tot++; return tot; } void solve(int s,int e) { vis[s]=1; dis[s]=0; int i,front=0,rear=1,lev=1,end=rear,tot=0; q[front]=s; while(front<rear) { int x=q[front]; front++; for(i=h[x];i!=-1;i=nxt[i]) { int y=node[i]; if(dis[y]==-1) { dis[y]=lev; pre[y]=x; if(y==e) { int t=y; tot=0; while(1) { route[tot++]=t; if(t==s)break; t=pre[t]; } for(tot=tot-1;tot>=0;tot--)printf("%s\n",word[route[tot]]); return; } q[rear]=y; rear++; } } if(front==end) { lev++; end=rear; } } printf("No solution.\n"); } int main() { char src[20],dest[20]; int T=0,i,tot=0; memset(h,-1,sizeof(h)); memset(nxt,-1,sizeof(nxt)); while(gets(word[cnt])!=NULL) { if(word[cnt][0]=='\0') break; int la=strlen(word[cnt]); for(i=0;i<cnt;i++) { int lb=strlen(word[i]); if(la!=lb)continue; if(_strcmp(word[i],word[cnt],la)==1) { node[tot]=cnt; nxt[tot]=h[i]; h[i]=tot; tot++; node[tot]=i; nxt[tot]=h[cnt]; h[cnt]=tot; tot++; } } cnt++; } while(scanf("%s",src)!=EOF) { scanf("%s",dest); if(T)printf("\n"); memset(vis,0,sizeof(vis)); memset(dis,-1,sizeof(dis)); memset(pre,-1,sizeof(pre)); int sta=GetInd(src); int end=GetInd(dest); if(sta==-1||end==-1) { printf("No solution.\n"); continue; } solve(sta,end); T++; } return 0; }
View Code
相关文章推荐
- UVaOJ 112道题目-排序
- UVaOJ 112道题目-算数与代数
- 《挑战编程》 Programming challenge (UVaOJ 112道题目)
- UVaOJ 112道题目-组合数学
- 九度OJ—题目1066:字符串排序
- 2014年亚马逊在线笔试题目及解法_9_24(字符串编辑距离升级版)
- 题目1029:魔咒词典(map使用以及字符串读取函数总结)
- 【九度OJ】题目1192:回文字符串 解题报告
- 华为OJ训练之0018-170105-字符串翻转(字符逆序题目)
- 字符串替换空格题目
- 又通过一道题目,替换字符串 —— 剑指Offer
- 题目113 字符串替换
- C语言学习序列之面试题目-计算两个字符串中出现的相同英文字符
- 【C语言【面试题】【笔试题】题目:在字符串中找出第一个只出现一次的字符。
- UvaOj 题目难度分析表
- 2008-06-28 14:31 [汇总]字符串题目推荐及解题报告[转]
- 九度OJ题目1054:字符串内排序
- 码农谷题目之将字符串中的字符按规则重新排列
- 华为机试小题目之保留字符串中的字母和数字