0% found this document useful (0 votes)
75 views

Write A Program To Implement Doublets Algorithm C++ 11 Code

This C++ program implements the Doublets word game algorithm. It takes in a dictionary of words and builds a graph where each word is a node connected to other words that differ by one letter. It then performs BFS searches between start and end words to find the shortest path changing one letter at a time. The main steps are: building the graph by connecting words with a single letter difference, performing BFS searches from the end word to the start word, and outputting the path of words between them if found.

Uploaded by

snehal patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

Write A Program To Implement Doublets Algorithm C++ 11 Code

This C++ program implements the Doublets word game algorithm. It takes in a dictionary of words and builds a graph where each word is a node connected to other words that differ by one letter. It then performs BFS searches between start and end words to find the shortest path changing one letter at a time. The main steps are: building the graph by connecting words with a single letter difference, performing BFS searches from the end word to the start word, and outputting the path of words between them if found.

Uploaded by

snehal patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a program to implement Doublets algorithm

C++ 11 Code:
#include <stdio.h>
#include <string.h>
#include <vector>
#include <queue>
#include <iostream>
#include <map>
using namespace std;
char word[25145][17];
int n = 0;
vector<int> g[25145], bucket[17];
map<string, int> S2N[17];
int isDoublet(int x, int y, int len) {
static int i, cnt;
for(i = 0, cnt = 0; i < len; i++) {
cnt += word[x][i] != word[y][i];
if(cnt > 1)
return 0;
}
return cnt == 1;
}
void build_graph() {
int i, j, k, m, x, y;
for(i = 1; i <= 16; i++) {
m = bucket[i].size();
for(j = 0; j < m; j++) {
for(k = j+1; k < m; k++) {
x = bucket[i][j], y = bucket[i][k];
if(isDoublet(x, y, i)) {
g[x].push_back(y);
g[y].push_back(x);
}
}
}
}
}
int pre[25145];
void bfs(int st, int ed) {
queue<int> Q;
char used[25145] = {};
Q.push(st), used[st] = 1, pre[st] = -1;
while(!Q.empty()) {
st = Q.front(), Q.pop();
for(vector<int>::iterator it = g[st].begin();
it != g[st].end(); it++) {
if(!used[*it]) {
used[*it] = 1;
pre[*it] = st;
Q.push(*it);
}
}
if(used[ed])
break;
}
if(!used[ed])
puts("No solution.");
else {
while(ed >= 0) {
puts(word[ed]);
ed = pre[ed];
}
}
}
int main() {
int word_len, st_len, ed_len;
while(gets(word[n]) && word[n][0] != '\0') {
word_len = strlen(word[n]);
bucket[word_len].push_back(n);
S2N[word_len][word[n]] = n;
n++;
}
build_graph();
char st[20], ed[20];
int first = 0, x, y;
while(scanf("%s %s", &st, &ed) == 2) {
if(first) puts("");
first = 1;
st_len = strlen(st);
ed_len = strlen(ed);
if(st_len != ed_len) {
puts("No solution.");
continue;
}
x = S2N[st_len][st];
y = S2N[ed_len][ed];
bfs(y, x);
}
return 0;
}
Sample Input:
booster
rooster
roaster
coasted
roasted
coastal
postal
booster roasted
coastal postal

Sample Output:
booster
rooster
roaster
roasted
No solution.

ONLINE JUDGE ACCEPTED OUTPUT:

You might also like