标签:
题意:给两个字符串A,B,A和B都含有小写英文字母,同时都额外含有且仅含有一个字符
解法:让A串作为
My Code
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
class TwoStringMasks{
public:
string shortestCommon(string s1, string s2){
int t1 = 0, t2 = 0;
for(int i = 0; i < s1.length(); i++) if(s1[i] == ‘*‘) t1 = i;
for(int i = 0; i < s2.length(); i++) if(s2[i] == ‘*‘) t2 = i;
if(t1 > t2) swap(s1, s2);
string ans1 = "", ans3 = "";
for(int i = 0; i < s1.length(); i++) if(s1[i] == ‘*‘) ans1 = s1.substr(0, i);
for(int i = 1; i < s2.length(); i++) if(s2[i-1] == ‘*‘) ans3 = s2.substr(i, s2.length() - i);
while(s1[0] != ‘*‘ && s2[0] != ‘*‘)
{
if(s1[0] != s2[0]) return "impossible";
s1.erase(0, 1), s2.erase(0, 1);
}
while(s1.back() != ‘*‘ && s2.back() != ‘*‘)
{
if(s1.back() != s2.back()) return "impossible";
s1.pop_back(), s2.pop_back();
}
s1.erase(0, 1);
string ans = ".";
for(int i = -1; i < (int)s2.length(); i++)
{
if(i >= 0 && s2[i] == ‘*‘) break;
string s3; s3.clear();
for(int j = 0; j <= i; j++) s3 += s2[j];
s3 += s1;
bool ok = 1;
int t3 = 0, t2 = 0;
while(t3 < s3.length() && t2 < s2.length() && ok)
{
if(s2[t2] == ‘*‘) break;
else if(s3[t3] != s2[t2]) ok = 0;
else t3++, t2++;
}
if(s2[t2] != ‘*‘) ok = 0;
if(ok && (ans == "." || ans.length() > (ans1 + s3 + ans3).length()))
{
ans = ans1 + s3 + ans3;
}
}
return ans;
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/uestc_peterpan/article/details/47716217