SRM 391(1-250pt)
DIV1 250pt
题意:给两个'a'-'z'的字符串,是否存在一个'a'-'z'的置换,使得能将一个字符串转化成另一个字符串。
解法:题意即是求,s1和s2对应位置出现的字符在原字符串中出现的次数和每次出现的位置是否一样。可以将s1和s2分别转化成一个数列,数列的相等与否 和 是否存在那样的置换是等价的。
tag:think
1 // BEGIN CUT HERE 2 /* 3 4 */ 5 // END CUT HERE 6 #line 7 "IsomorphicWords.cpp" 7 #include <cstdlib> 8 #include <cctype> 9 #include <cstring> 10 #include <cstdio> 11 #include <cmath> 12 #include <algorithm> 13 #include <vector> 14 #include <iostream> 15 #include <sstream> 16 #include <set> 17 #include <queue> 18 #include <fstream> 19 #include <numeric> 20 #include <iomanip> 21 #include <bitset> 22 #include <list> 23 #include <stdexcept> 24 #include <functional> 25 #include <string> 26 #include <utility> 27 #include <map> 28 #include <ctime> 29 #include <stack> 30 31 using namespace std; 32 33 #define clr0(x) memset(x, 0, sizeof(x)) 34 #define clr1(x) memset(x, -1, sizeof(x)) 35 #define pb push_back 36 #define sz(v) ((int)(v).size()) 37 #define out(x) cout<<#x<<":"<<(x)<<endl 38 #define tst(a) cout<<#a<<endl 39 #define CINBEQUICKER std::ios::sync_with_stdio(false) 40 41 typedef vector<int> VI; 42 typedef vector<string> VS; 43 typedef vector<double> VD; 44 typedef long long int64; 45 46 const double eps = 1e-8; 47 const double PI = atan(1.0)*4; 48 const int inf = 2139062143 / 2; 49 50 inline int MyMod( int a , int b ) { return (a%b+b)%b;} 51 52 void gao(string a, int *num) 53 { 54 int n = sz(a); 55 map<char, int> mp; mp.clear(); 56 int cnt = 0; 57 for (int i = 0; i < n; ++ i){ 58 if (!mp.count(a[i])){ 59 mp[a[i]] = cnt; num[i] = cnt ++; 60 } 61 else num[i] = mp[a[i]]; 62 } 63 } 64 65 bool ok(string a, string b) 66 { 67 int n1[100], n2[100]; 68 clr0 (n1); clr0 (n2); 69 gao(a, n1); gao(b, n2); 70 for (int i = 0; i < 100; ++ i) 71 if (n1[i] != n2[i]) return 0; 72 return 1; 73 } 74 75 class IsomorphicWords 76 { 77 public: 78 int countPairs(vector <string> v){ 79 int ret = 0; 80 for (int i = 0; i < sz(v); ++ i) 81 for (int j = i+1; j < sz(v); ++ j) 82 if (ok(v[i], v[j])) ++ ret; 83 return ret; 84 } 85 86 // BEGIN CUT HERE 87 public: 88 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(); } 89 private: 90 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(); } 91 void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 92 void test_case_0() { string Arr0[] = {"abca", "zbxz", "opqr"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 1; verify_case(0, Arg1, countPairs(Arg0)); } 93 void test_case_1() { string Arr0[] = {"aa", "ab", "bb", "cc", "cd"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 4; verify_case(1, Arg1, countPairs(Arg0)); } 94 void test_case_2() { string Arr0[] = { "cacccdaabc", "cdcccaddbc", "dcdddbccad", "bdbbbaddcb", 95 "bdbcadbbdc", "abaadcbbda", "babcdabbac", "cacdbaccad", 96 "dcddabccad", "cacccbaadb", "bbcdcbcbdd", "bcbadcbbca" }; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arg1 = 13; verify_case(2, Arg1, countPairs(Arg0)); } 97 98 // END CUT HERE 99 100 }; 101 //by plum rain 102 // BEGIN CUT HERE 103 int main() 104 { 105 //freopen( "a.out" , "w" , stdout ); 106 IsomorphicWords ___test; 107 ___test.run_test(-1); 108 return 0; 109 } 110 // END CUT HERE
------------------------------------------------------------------
现在的你,在干什么呢?
你是不是还记得,你说你想成为岩哥那样的人。
现在的你,在干什么呢?
你是不是还记得,你说你想成为岩哥那样的人。