zoukankan      html  css  js  c++  java
  • SRM 392(1-250pt)

    DIV1 250pt

    题意:给两个各含有一个*号的字符串s1和s2,可以用一个任意字符串代替*号(注意是串,不是只能用单个字符代替,也可以为用空串代替),问能否将s1和s2变为相同的字符串。如果能输出改变后长度最短的方案。

    解法:其实是很简单的暴力。。。。我代码写的慢有两个原因,一是string的substr不会用,另一个是因为s1和s2地位对等,可以通过交换他们的位置来省代码。

    tag:brute-force

      1 // BEGIN CUT HERE
      2 /*
      3  * Author:  plum rain
      4  * score :
      5  */
      6 /*
      7 
      8  */
      9 // END CUT HERE
     10 #line 11 "TwoStringMasks.cpp"
     11 #include <sstream>
     12 #include <stdexcept>
     13 #include <functional>
     14 #include <iomanip>
     15 #include <numeric>
     16 #include <fstream>
     17 #include <cctype>
     18 #include <iostream>
     19 #include <cstdio>
     20 #include <vector>
     21 #include <cstring>
     22 #include <cmath>
     23 #include <algorithm>
     24 #include <cstdlib>
     25 #include <set>
     26 #include <queue>
     27 #include <bitset>
     28 #include <list>
     29 #include <string>
     30 #include <utility>
     31 #include <map>
     32 #include <ctime>
     33 #include <stack>
     34 
     35 using namespace std;
     36 
     37 #define clr0(x) memset(x, 0, sizeof(x))
     38 #define clr1(x) memset(x, -1, sizeof(x))
     39 #define pb push_back
     40 #define sz(v) ((int)(v).size())
     41 #define all(t) t.begin(),t.end()
     42 #define zero(x) (((x)>0?(x):-(x))<eps)
     43 #define out(x) cout<<#x<<":"<<(x)<<endl
     44 #define tst(a) cout<<a<<" "
     45 #define tst1(a) cout<<#a<<endl
     46 #define CINBEQUICKER std::ios::sync_with_stdio(false)
     47 
     48 typedef vector<int> vi;
     49 typedef vector<string> vs;
     50 typedef vector<double> vd;
     51 typedef pair<int, int> pii;
     52 typedef long long int64;
     53 
     54 const double eps = 1e-8;
     55 const double PI = atan(1.0)*4;
     56 const int inf = 2139062143 / 2;
     57 
     58 string temp = "impossible";
     59 
     60 string gao(string s1, string s2)
     61 {
     62     int len = sz(s1);
     63     string ans;
     64     for (int i = len; i; -- i){
     65         int t1 = 0, t2 = sz(s2) - i;
     66         bool ok = 1;
     67         while (t2 < sz(s2) && t1 < len){
     68             if (s1[t1] != s2[t2]){
     69                 ok = 0; break;
     70             }
     71             ++ t1; ++ t2;
     72         }
     73         if (ok){
     74             for (int j = i; j < len; ++ j)
     75                 s2.pb (s1[j]);
     76             return s2;
     77         }
     78     }
     79     return s2 + s1;
     80 }
     81 
     82 string Out(string s)
     83 {
     84     string ans;
     85     for (int i = 0; i < sz(s); ++ i)
     86         if (s[i] != '*') ans.pb (s[i]);
     87     return ans;
     88 }
     89 
     90 class TwoStringMasks
     91 {
     92     public:
     93         string shortestCommon(string s1, string s2){
     94             int i1 = 0, i2 = sz(s1)-1, j1 = 0, j2 = sz(s2)-1;
     95             while (s1[i1] != '*' && s2[j1] != '*'){
     96                 if (s1[i1] == s2[j1]) ++ i1, ++ j1;
     97                 else return temp;
     98             }
     99             while (s1[i2] != '*' && s2[j2] != '*'){
    100                 if (s1[i2] == s2[j2]) -- i2, -- j2;
    101                 else return temp;
    102             }
    103 
    104             if (s1[i1] == s1[i2] && s1[i1] == '*') return Out(s2);
    105             if (s2[j1] == s2[j2] && s2[j1] == '*') return Out(s1);
    106             
    107             if (s1[i1] == '*' && s1[i2] != '*'){
    108                 string t1, t2, g1, g2;
    109                 for (int i = 0; i < i1; ++ i) t1.pb (s1[i]);
    110                 for (int i = i2+1; i < sz(s1); ++ i) t2.pb (s1[i]);
    111                 for (int i = i1+1; i <= i2; ++ i) g1.pb (s1[i]);
    112                 for (int i = j1; i < j2; ++ i) g2.pb (s2[i]);
    113                 return t1 + gao(g1, g2) + t2;
    114             }
    115             if (s1[i1] != '*' && s1[i2] == '*'){
    116                 string t1, t2, g1, g2;
    117                 for (int i = 0; i < i1; ++ i) t1.pb (s1[i]);
    118                 for (int i = i2+1; i < sz(s1); ++ i) t2.pb (s1[i]);
    119                 for (int i = i1; i < i2; ++ i) g1.pb (s1[i]);
    120                 for (int i = j1+1; i <= j2; ++ i) g2.pb (s2[i]);
    121                 return t1 + gao(g2, g1) + t2;
    122             }
    123             return temp;
    124         }
    125         
    126 // BEGIN CUT HERE
    127     public:
    128     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); if ((Case == -1) || (Case == 7)) test_case_7(); }
    129     //void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_6();}
    130     private:
    131     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(); }
    132     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 << "	Expected: "" << Expected << '"' << endl; cerr << "	Received: "" << Received << '"' << endl; } }
    133     void test_case_0() { string Arg0 = "TOPC*DER"; string Arg1 = "T*PCODER"; string Arg2 = "TOPCODER"; verify_case(0, Arg2, shortestCommon(Arg0, Arg1)); }
    134     void test_case_1() { string Arg0 = "HELLO*"; string Arg1 = "HI*"; string Arg2 = "impossible"; verify_case(1, Arg2, shortestCommon(Arg0, Arg1)); }
    135     void test_case_2() { string Arg0 = "GOOD*LUCK"; string Arg1 = "*"; string Arg2 = "GOODLUCK"; verify_case(2, Arg2, shortestCommon(Arg0, Arg1)); }
    136     void test_case_3() { string Arg0 = "*SAMPLETEST"; string Arg1 = "THIRDSAMPLE*"; string Arg2 = "THIRDSAMPLETEST"; verify_case(3, Arg2, shortestCommon(Arg0, Arg1)); }
    137     void test_case_4() { string Arg0 = "*TOP"; string Arg1 = "*CODER"; string Arg2 = "impossible"; verify_case(4, Arg2, shortestCommon(Arg0, Arg1)); }
    138     void test_case_5() { string Arg0 = "*"; string Arg1 = "A*"; string Arg2 = "A"; verify_case(5, Arg2, shortestCommon(Arg0, Arg1)); }
    139     void test_case_6() { string Arg0 = "*A"; string Arg1 = "B*"; string Arg2 = "BA"; verify_case(6, Arg2, shortestCommon(Arg0, Arg1)); }
    140     void test_case_7() { string Arg0 = "LASTCASE*"; string Arg1 = "*LASTCASE"; string Arg2 = "LASTCASE"; verify_case(7, Arg2, shortestCommon(Arg0, Arg1)); }
    141 
    142 // END CUT HERE
    143 
    144 };
    145 
    146 // BEGIN CUT HERE
    147 int main()
    148 {
    149 //    freopen( "a.out" , "w" , stdout );    
    150     TwoStringMasks ___test;
    151     ___test.run_test(-1);
    152        return 0;
    153 }
    154 // END CUT HERE
    View Code
  • 相关阅读:
    password
    bzoj 1458: 士兵占领
    国家集训队2011 happiness
    cogs 2051. 王者之剑
    uva 10779 Collectors Problem
    [Jxoi2012]奇怪的道路
    天神下凡
    藏宝图
    黑红树
    愤怒的小鸟
  • 原文地址:https://www.cnblogs.com/plumrain/p/srm_392.html
Copyright © 2011-2022 走看看