Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <stdio.h>
- #include <array>
- using namespace std;
- string phone(char number[], int num, string words[]);
- string** find(string* vars[], string curD, string curDN, string words[], int* n,int num,int length, int s);
- int main()
- {
- string tem = "start";
- int numStr;
- int cntr = 0;
- char *number = new char[0];
- string *words = new string[0];
- string numSrt;
- while (tem != "-1")
- {
- cin >> tem;
- if (tem != "-1")
- {
- if (cntr != 0)
- cout << endl;
- strcpy(number, tem.c_str());
- cin >> tem;
- numStr = stoi(tem);
- words = new string[numStr];
- for (int i = 0; i < numStr; i++)
- {
- cin >> words[i];
- }
- numSrt = phone(number, numStr, words);
- cout << numSrt;
- cntr = cntr + 1;
- };
- };
- return 0;
- }
- string phone(char number[], int num, string words[])
- {
- for (int i = 0; i < num; i++)
- {
- }
- int length = strlen(number);
- string **vars = new string*[2];
- vars[0] = new string[pow(3, num)];
- vars[1] = new string[pow(3, num)];
- string **varsR = new string*[2];
- varsR[0] = new string[0];
- varsR[1] = new string[0];
- string **varsD = new string*[2];
- varsD[0] = new string[0];
- varsD[1] = new string[0];
- int s = 1;
- string curSS;
- string curSN;
- int c = 0;
- for (int i = 0; i < num; i++)
- {
- s = 0;
- curSS = words[i];
- curSN = to_string(i + 1);
- if (curSS.length() == length)
- {
- vars[0][c] = curSS;
- vars[1][c] = curSN;
- c = c + 1;
- }
- else
- {
- vars = find(vars, curSS, curSN, words, &c, num, length, s);
- };
- }
- char* chV = new char[0];
- char* chW = new char[0];
- int tst = 1;
- char cur;
- int r = 0;
- size_t pos;
- string str;
- string curS;
- for (int i = 0; i < pow(3,num); i++)
- {
- if (vars[0][i] != "")
- {
- varsD[0] = new string[i + 1];
- varsD[1] = new string[i + 1];
- }
- };
- for (int i = 0; i < pow(3, num);i++)
- {
- if (vars[0][i] != "")
- {
- tst = 1;
- for (int ii = 0; ii < num;ii++)
- {
- if (tst > 0)
- {
- str = vars[0][i];
- curS = words[ii];
- pos = str.find(curS);
- if (pos != string::npos)
- {
- str.erase(pos, strlen(curS.c_str()));
- if (str.find(curS, pos) != string::npos)
- {
- tst = tst - num;
- }
- }
- }
- }
- if (tst > 0)
- {
- varsD[0][r] = vars[0][i];
- varsD[1][r] = vars[1][i];
- r = r + 1;
- varsR[0] = new string[r];
- varsR[1] = new string[r];
- }
- }
- }
- int rr = 0;
- for (int i = 0; i < r; i++)
- {
- if (varsD[0][i] != "")
- {
- tst = 1;
- strcpy(chV, varsD[0][i].c_str());
- for (int ii = 0; ii < strlen(chV);ii++)
- {
- if (tst > 0)
- {
- cur = chV[ii];
- if (number[ii] == '0')
- if (cur != 'q' && cur != 'o' && cur != 'z')
- tst = tst - 5;
- if (number[ii] == '1')
- if (cur != 'i' && cur != 'j')
- tst = tst - 5;
- if (number[ii] == '2')
- if (cur != 'a' && cur != 'b' && cur != 'c')
- tst = tst - 5;
- if (number[ii] == '3')
- if (cur != 'd' && cur != 'e' && cur != 'f')
- tst = tst - 5;
- if (number[ii] == '4')
- if (cur != 'g' && cur != 'h')
- tst = tst - 5;
- if (number[ii] == '5')
- if (cur != 'k' && cur != 'l')
- tst = tst - 5;
- if (number[ii] == '6')
- if (cur != 'm' && cur != 'n')
- tst = tst - 5;
- if (number[ii] == '7')
- if ((cur != 'r') && (cur != 'p') && (cur != 's'))
- tst = tst - 5;
- if (number[ii] == '8')
- if (cur != 't' && cur != 'u' && cur != 'v')
- tst = tst - 5;
- if (number[ii] == '9')
- if (cur != 'w' && cur != 'x' && cur != 'y')
- tst = tst - 5;
- }
- }
- if (tst > 0)
- {
- varsR[0][rr] = varsD[0][i];
- varsR[1][rr] = varsD[1][i];
- rr = rr + 1;
- }
- }
- };
- int min = num + num - 1;
- int count = 0;
- string varsE = "";
- string returnE = "No solution.";
- string varsEN = "";
- string curE = "";
- int curEN = 0;
- for (int i = 0; i < rr;i++)
- {
- if (varsR[0][i] != "")
- {
- str = varsR[1][i];
- strcpy(chV, str.c_str());
- count = 0;
- curEN = 0;
- varsEN = "";
- varsE = "";
- for (int ii = 0; ii <= strlen(chV); ii++)
- {
- if (chV[ii] != ' ' && ii != strlen(chV))
- {
- curE = curE + string(1, chV[ii]);
- }
- else
- {
- curEN = stoi(curE);
- count = count + 1;
- if (varsEN != "")
- {
- varsEN = varsEN + " " + curE;
- }
- else
- {
- varsEN = curE;
- }
- if (varsE != "")
- {
- varsE = varsE + " " + words[curEN-1];
- }
- else
- {
- varsE = words[curEN-1];
- }
- curE = "";
- }
- if (count <= min)
- {
- min = count;
- returnE = varsE;
- }
- }
- }
- }
- return returnE;
- }
- string** find(string* vars[], string curD, string curDN, string words[], int* n,int num, int length, int s)
- {
- string cur;
- string curN;
- for (int i = 0;i < num;i++)
- {
- cur = curD;
- curN = curDN;
- if ((cur != words[i]) && ((cur + words[i]).length() <= length))
- {
- cur = cur + words[i];
- curN = curN + " " + to_string(i + 1);
- if (cur.length() == length)
- {
- vars[0][*n] = cur;
- vars[1][*n] = curN;
- *n = *n + 1;
- } else
- {
- vars = find(vars, cur, curN, words, n, num, length, s);
- };
- };
- };
- return vars;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement