Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- struct myPair {
- std::string replaceStr;
- std::string toReplace;
- };
- bool myComp(const myPair& a, const myPair& b) {
- return a.replaceStr < b.replaceStr;
- }
- std::string genStrWithLowestLen(std::string toGen, std::vector<myPair> v) {
- using namespace std;
- string tmp;
- string smallestStr = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
- do {
- tmp = toGen;
- int left = -1;
- for (int i(0); i < v.size(); ++i) {
- left = tmp.find(v[i].replaceStr, left + 1);
- while (left != -1) {
- tmp.replace(left, v[i].replaceStr.length(), v[i].toReplace);
- left = tmp.find(v[i].replaceStr, left + 1);
- }
- }
- int l1 = tmp.length(), l2 = smallestStr.length();
- if (l1 < l2 and !tmp.empty()) {
- smallestStr = tmp;
- }
- tmp = "";
- } while (next_permutation(v.begin(), v.end(), myComp));
- if (smallestStr.length() < 255)
- return smallestStr;
- return toGen;
- return " ";
- }
- int main() {
- using namespace std;
- int replaceStrCount, stringCount;
- cin >> replaceStrCount >> stringCount;
- vector<myPair> vec(replaceStrCount);
- string a;
- getline(cin, a);
- for (int i = 0; i < replaceStrCount; i++) {
- string tmp;
- getline(cin, tmp);
- vec[i].replaceStr = tmp;
- getline(cin, tmp);
- vec[i].toReplace = tmp;
- }
- string resultStr;
- for (int i = 0; i < stringCount; i++) {
- string tmpStr;
- getline(cin, tmpStr);
- if (tmpStr.empty()) {
- resultStr += '\n' + tmpStr;
- }
- resultStr += "\n" + genStrWithLowestLen(tmpStr, vec);
- }
- cout << resultStr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement