summaryrefslogtreecommitdiff
path: root/429
diff options
context:
space:
mode:
authorAditya Naik2019-03-01 06:03:21 -0500
committerAditya Naik2019-03-01 06:03:21 -0500
commit098415483c31f06faa04200c08a7112c28c6c0ec (patch)
treedb7683e5d3629a32414b40842dccbc9c2295f382 /429
parentede1a38950db4778b20573a05c8de50c2458c958 (diff)
429 AC
Diffstat (limited to '429')
-rw-r--r--429/in72
-rw-r--r--429/in27
-rw-r--r--429/word.c25
-rw-r--r--429/word.cpp52
-rw-r--r--429/word.in67
5 files changed, 43 insertions, 180 deletions
diff --git a/429/in b/429/in
index b79b8e8..7f0c46c 100644
--- a/429/in
+++ b/429/in
@@ -1,67 +1,13 @@
-4
+2
-dip
-lip
-mad
-map
-maple
-may
-pad
-pip
-pod
-pop
-sap
-sip
-slice
-slick
-spice
-stick
-stock
+aa
+bb
+ac
*
-spice stock
-may pod
+aa ac
-dip
-lip
-mad
-map
-mapl
-maple
-may
-pad
-pip
-pod
-pop
-sap
-sip
-slice
-slick
-spice
-stick
-stock
+aa
+ab
+bb
*
-spice slick
-may mad
-map maple
-maple map
-map map
-
-mapl
-maple
-mba
-mbal
-mbale
-mbple
-*
-mba mapl
-
-map
-mapl
-maple
-mba
-mbal
-mbale
-mbp
-mbple
-*
-mba mapl
+aa bb
diff --git a/429/in2 b/429/in2
deleted file mode 100644
index 8adc3ce..0000000
--- a/429/in2
+++ /dev/null
@@ -1,7 +0,0 @@
-1
-
-pip
-slkdj
-sdwi
-sdkj
-* \ No newline at end of file
diff --git a/429/word.c b/429/word.c
deleted file mode 100644
index f8363aa..0000000
--- a/429/word.c
+++ /dev/null
@@ -1,25 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-int main(){
- int tc;
- char *word = malloc(10);
- size_t st = 10;
- scanf("%d", &tc);
- while(tc--){
- getline(&word, &st, stdin); /* blank line after tc number */
- char dict[200][10];
- int i = 0;
-
- getline(&word, &st, stdin);
- while(word[0]!='*'){
- memcpy(dict[i++], word, st);
- getline(&word, &st, stdin);
- }
- int j=0;
- for(;j<i; j++){
- printf("%s", dict[j]);
- }
- }
-}
diff --git a/429/word.cpp b/429/word.cpp
index 6685b17..066b59c 100644
--- a/429/word.cpp
+++ b/429/word.cpp
@@ -5,8 +5,8 @@
#include <sstream>
using namespace std;
-int get_dist(string a, string b);
-void make_graph(vector<string> wlist, int map[][200]);
+void makeGraph(vector<string> wlist, int map[][200]);
+int getDist(string a, string b);
int bfs(int map[][200], int start, int dest, int s);
int main(){
@@ -15,31 +15,36 @@ int main(){
while(tc--){
string w="";
vector<string> wlist;
- int map[200][200];
+ int map[200][200] = {0};
+ cin>>w;
while(w!="*"){
- cin>>w;
wlist.push_back(w);
+ cin>>w;
}
- make_graph(wlist, map);
+ makeGraph(wlist, map);
cin.ignore();
string line, s1, s2;
getline(cin, line);
while(line!=""){
- int l1, l2;
+ int l1, l2; /* indices to words in map */
istringstream iss(line);
iss>>s1>>s2;
for(int i=0; i<wlist.size(); i++){
if(wlist[i]==s1)
l1 = i;
- else if(wlist[i]==s2)
+ if(wlist[i]==s2)
l2 = i;
}
-
- cout<<s1<<" "<<s2<<" "<<bfs(map, l1, l2, wlist.size())<<"\n";
+ int dist;
+
+ dist = bfs(map, l1, l2, wlist.size());
+ cout<<s1<<" "<<s2<<" "<<dist<<"\n";
getline(cin, line);
}
+ if(tc>0)
+ cout<<endl;
}
}
@@ -58,7 +63,7 @@ while !q.empty
int bfs(int map[][200], int start, int dest, int s){
queue<int> q;
int pred[200], n;
- bool visited[200] = {false};
+ bool visited[200] = {false};
q.push(start);
while(!q.empty()){
@@ -81,21 +86,32 @@ int bfs(int map[][200], int start, int dest, int s){
return len;
}
-void make_graph(vector<string> wlist, int map[][200]){
+void makeGraph(vector<string> wlist, int map[][200]){
for(int i=0; i<wlist.size(); i++)
for(int j=0; j<wlist.size(); j++)
- if(i!=j && get_dist(wlist[i], wlist[j])==1){
+ if(i!=j && getDist(wlist[i], wlist[j])==1){
map[i][j] = 1;
map[j][i] = 1;
}
}
-int get_dist(string a, string b){
+int getDist(string a, string b){
int dist = 0;
- int len = a.length();
- if(len == b.length())
- for(int i=0; i<len; i++)
- if(a[i]!=b[i])
- dist++;
+ string longer, shorter;
+
+ if(a.length() > b.length()){
+ longer = a;
+ shorter = b;
+ }
+ else{ /* Also accounts for equal length */
+ longer = b;
+ shorter = a;
+ }
+ dist += longer.length() - shorter.length();
+
+ for(int i=0; i<shorter.length(); i++)
+ if(shorter[i]!=longer[i])
+ dist+=1;
+
return dist;
}
diff --git a/429/word.in b/429/word.in
deleted file mode 100644
index b79b8e8..0000000
--- a/429/word.in
+++ /dev/null
@@ -1,67 +0,0 @@
-4
-
-dip
-lip
-mad
-map
-maple
-may
-pad
-pip
-pod
-pop
-sap
-sip
-slice
-slick
-spice
-stick
-stock
-*
-spice stock
-may pod
-
-dip
-lip
-mad
-map
-mapl
-maple
-may
-pad
-pip
-pod
-pop
-sap
-sip
-slice
-slick
-spice
-stick
-stock
-*
-spice slick
-may mad
-map maple
-maple map
-map map
-
-mapl
-maple
-mba
-mbal
-mbale
-mbple
-*
-mba mapl
-
-map
-mapl
-maple
-mba
-mbal
-mbale
-mbp
-mbple
-*
-mba mapl