DIV1 250pt
题意:给两个各含有一个*号的字符串s1和s2,可以用一个任意字符串代替*号(注意是串,不是只能用单个字符代替,也可以为用空串代替),问能否将s1和s2变为相同的字符串。如果能输出改变后长度最短的方案。
解法:其实是很简单的暴力。。。。我代码写的慢有两个原因,一是string的substr不会用,另一个是因为s1和s2地位对等,可以通过交换他们的位置来省代码。
tag:brute-force
// BEGIN CUT HERE
/*
* Author: plum rain
* score :
*/
/* */
// END CUT HERE
#line 11 "TwoStringMasks.cpp"
#include <sstream>
#include <stdexcept>
#include <functional>
#include <iomanip>
#include <numeric>
#include <fstream>
#include <cctype>
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <queue>
#include <bitset>
#include <list>
#include <string>
#include <utility>
#include <map>
#include <ctime>
#include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x))
#define clr1(x) memset(x, -1, sizeof(x))
#define pb push_back
#define sz(v) ((int)(v).size())
#define all(t) t.begin(),t.end()
#define zero(x) (((x)>0?(x):-(x))<eps)
#define out(x) cout<<#x<<":"<<(x)<<endl
#define tst(a) cout<<a<<" "
#define tst1(a) cout<<#a<<endl
#define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, int> pii;
typedef long long int64; const double eps = 1e-;
const double PI = atan(1.0)*;
const int inf = / ; string temp = "impossible"; string gao(string s1, string s2)
{
int len = sz(s1);
string ans;
for (int i = len; i; -- i){
int t1 = , t2 = sz(s2) - i;
bool ok = ;
while (t2 < sz(s2) && t1 < len){
if (s1[t1] != s2[t2]){
ok = ; break;
}
++ t1; ++ t2;
}
if (ok){
for (int j = i; j < len; ++ j)
s2.pb (s1[j]);
return s2;
}
}
return s2 + s1;
} string Out(string s)
{
string ans;
for (int i = ; i < sz(s); ++ i)
if (s[i] != '*') ans.pb (s[i]);
return ans;
} class TwoStringMasks
{
public:
string shortestCommon(string s1, string s2){
int i1 = , i2 = sz(s1)-, j1 = , j2 = sz(s2)-;
while (s1[i1] != '*' && s2[j1] != '*'){
if (s1[i1] == s2[j1]) ++ i1, ++ j1;
else return temp;
}
while (s1[i2] != '*' && s2[j2] != '*'){
if (s1[i2] == s2[j2]) -- i2, -- j2;
else return temp;
} if (s1[i1] == s1[i2] && s1[i1] == '*') return Out(s2);
if (s2[j1] == s2[j2] && s2[j1] == '*') return Out(s1); if (s1[i1] == '*' && s1[i2] != '*'){
string t1, t2, g1, g2;
for (int i = ; i < i1; ++ i) t1.pb (s1[i]);
for (int i = i2+; i < sz(s1); ++ i) t2.pb (s1[i]);
for (int i = i1+; i <= i2; ++ i) g1.pb (s1[i]);
for (int i = j1; i < j2; ++ i) g2.pb (s2[i]);
return t1 + gao(g1, g2) + t2;
}
if (s1[i1] != '*' && s1[i2] == '*'){
string t1, t2, g1, g2;
for (int i = ; i < i1; ++ i) t1.pb (s1[i]);
for (int i = i2+; i < sz(s1); ++ i) t2.pb (s1[i]);
for (int i = i1; i < i2; ++ i) g1.pb (s1[i]);
for (int i = j1+; i <= j2; ++ i) g2.pb (s2[i]);
return t1 + gao(g2, g1) + t2;
}
return temp;
} // BEGIN CUT HERE
public:
void run_test(int Case) { if ((Case == -) || (Case == )) test_case_0(); if ((Case == -) || (Case == )) test_case_1(); if ((Case == -) || (Case == )) test_case_2(); if ((Case == -) || (Case == )) test_case_3(); if ((Case == -) || (Case == )) test_case_4(); if ((Case == -) || (Case == )) test_case_5(); if ((Case == -) || (Case == )) test_case_6(); if ((Case == -) || (Case == )) test_case_7(); }
//void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_6();}
private:
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
void test_case_0() { string Arg0 = "TOPC*DER"; string Arg1 = "T*PCODER"; string Arg2 = "TOPCODER"; verify_case(, Arg2, shortestCommon(Arg0, Arg1)); }
void test_case_1() { string Arg0 = "HELLO*"; string Arg1 = "HI*"; string Arg2 = "impossible"; verify_case(, Arg2, shortestCommon(Arg0, Arg1)); }
void test_case_2() { string Arg0 = "GOOD*LUCK"; string Arg1 = "*"; string Arg2 = "GOODLUCK"; verify_case(, Arg2, shortestCommon(Arg0, Arg1)); }
void test_case_3() { string Arg0 = "*SAMPLETEST"; string Arg1 = "THIRDSAMPLE*"; string Arg2 = "THIRDSAMPLETEST"; verify_case(, Arg2, shortestCommon(Arg0, Arg1)); }
void test_case_4() { string Arg0 = "*TOP"; string Arg1 = "*CODER"; string Arg2 = "impossible"; verify_case(, Arg2, shortestCommon(Arg0, Arg1)); }
void test_case_5() { string Arg0 = "*"; string Arg1 = "A*"; string Arg2 = "A"; verify_case(, Arg2, shortestCommon(Arg0, Arg1)); }
void test_case_6() { string Arg0 = "*A"; string Arg1 = "B*"; string Arg2 = "BA"; verify_case(, Arg2, shortestCommon(Arg0, Arg1)); }
void test_case_7() { string Arg0 = "LASTCASE*"; string Arg1 = "*LASTCASE"; string Arg2 = "LASTCASE"; verify_case(, Arg2, shortestCommon(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE
int main()
{
// freopen( "a.out" , "w" , stdout );
TwoStringMasks ___test;
___test.run_test(-);
return ;
}
// END CUT HERE