Move solution into a separate function
Doing this greatly improves code readability.
void solve() {
int NumOfStrings;
/* ... */
}
int main() {
int t;
cin >> t;
while (t--)
solve();
return 0;
}
Be careful about using resources
On each iteration, we do a lot of redundant work by creating and destroying 100k strings.
for (int i = 0; i < tests; i++) { //one loop per each test case
string words[100000];
}
Note, that heap allocation is not involved due to the small string optimization.
To optimize memory usage, make the array static.
for (int i = 0; i < tests; i++) { //one loop per each test case
static string words[100000];
}
Misleading hint
Before checking the author's solution, I didn't quite understand the purpose of using map.
Let's find it out.
void solve() {
/* ... */
map<string, bool> mp;
for (int i = 0; i < n; i++) {
cin >> s[i];
mp[s[i]] = true; // map input string to true
}
for (int i = 0; i < n; i++) {
/* ... */
/* mp[pref] adds pair {pref, false} to mp
if pref is not presented and returns false;
otherwise, returns true if pref is an input string */
if (mp[pref] && mp[suff]) { ok = true; }
/* ... */
}
The author's map gradually growths, but only input strings are mapped to true.
Looks like the hint was misinterpreted, which is not surprising because it was taken out of the context.
for (int k = 0; k < NumOfStrings; k++) { // loop for each string in the test case
/* ... */
if (phrases.find(partition1) != phrases.end() && phrases.find(partition2) != phrases.end()) { //if both substrings exist in the map, then value for word is 1.
phrases[words[k]] = true;
}
}
}
for (int j = 0; j < NumOfStrings; j++) {
int value = 0;
if (phrases[words[j]] == true) value = 1;
cout << value;
}
Needlessly changing mapped values to check them later in the separate loop, is a lot of redundant work.
Searching for partitions is the only crucial part here, which may be performed with a set instead of map.
In conclusion
It suffices to apply the proposed improvements to make your solution accepted.
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
void solve()
{
int NumOfStrings;
cin >> NumOfStrings;
unordered_set<string> phrases;
static string words[100000];
for (int j = 0; j < NumOfStrings; j++) { // adds all input strings for each test case into words and phrases
string temp;
cin >> temp;
phrases.insert(temp);
words[j] = temp;
}
for (int k = 0; k < NumOfStrings; k++) { // loop for each string in the test case
int len = words[k].length(); // len is length of current word
bool ok = false;
for (int o = 0; o < len; o++) { // loops through current word
string partition1 = "";
string partition2 = "";
for (int m = 0; m <= o; m++) {
partition1 += words[k][m];
}
for (int n = o+1; n < len; n++) {
partition2 += words[k][n];
}
if (phrases.find(partition1) != phrases.end() && phrases.find(partition2) != phrases.end()) { //if both substrings exist in the map, then value for word is 1.
ok = true;
break;
}
}
cout << ok;
}
cout << endl;
}
int main()
{
int tests;
cin >> tests;
while (tests--)
solve();
return 0;
}