Advertisement
Kaidul

UVa - 10069

Dec 16th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.59 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define MEMSET_INF 127
  6. #define MEMSET_HALF_INF 63
  7. #define stream istringstream
  8. #define rep(i,n) for(__typeof(n) i=0; i<(n); i++)
  9. #define repl(i,n) for(__typeof(n) i=1; i<=(n); i++)
  10. #define FOR(i,a,b) for(__typeof(b) i=(a); i<=(b); i++)
  11. #define INF (1<<30)
  12. #define PI acos(-1.0)
  13. #define pb push_back
  14. #define ppb pop_back
  15. #define all(x) x.begin(),x.end()
  16. #define mem(x,y) memset(x,y,sizeof(x))
  17. #define memsp(x) mem(x,MEMSET_INF)
  18. #define memdp(x) mem(x,-1)
  19. #define memca(x) mem(x,0)
  20. #define eps 1e-9
  21. #define pii pair<int,int>
  22. #define pmp make_pair
  23. #define ft first
  24. #define sd second
  25. #define vi vector<int>
  26. #define vpii vector<pii>
  27. #define si set<int>
  28. #define msi map<string , int >
  29. #define mis map<int , string >
  30. typedef long long i64;
  31. typedef unsigned long long ui64;
  32. /** function **/
  33. #define SDi(x) sf("%d", &x)
  34. #define SDl(x) sf("%lld", &x)
  35. #define SDs(x) sf("%s", x)
  36. #define SD2(x,y) sf("%d%d", &x, &y)
  37. #define SD3(x,y,z) sf("%d%d%d", &x, &y, &z)
  38. #define pf printf
  39. #define print(x) pf("%d ", x)
  40. #define sf scanf
  41. #define READ(f) freopen(f, "r", stdin)
  42. #define WRITE(f) freopen(f, "w", stdout)
  43.  
  44. const i64 INF64 = (i64)1E18;
  45.  
  46. template<class T> inline T gcd(T a,T b) {
  47.     if(a<0)return gcd(-a,b);
  48.     if(b<0)return gcd(a,-b);
  49.     return (b==0)?a:gcd(b,a%b);
  50. }
  51. template<class T> inline T lcm(T a,T b) {
  52.     if(a<0)return lcm(-a,b);
  53.     if(b<0)return lcm(a, -b);
  54.     return a*(b/gcd(a,b));
  55. }
  56. template<class T> inline T sqr(T x) {
  57.     return x*x;
  58. }
  59. template<class T> T power(T N,T P) {
  60.     return (P==0)?  1: N*power(N,P-1);
  61. }
  62. template<class T> bool inside(T a,T b,T c) {
  63.     return (b>=a && b<=c);
  64. }
  65. double _dist(double x1,double y1,double x2,double y2) {
  66.     return sqrt(sqr(x1-x2)+sqr(y1-
  67.                                y2));
  68. }
  69. int distsq(int x1,int y1,int x2,int y2) {
  70.     return sqr(x1-x2)+sqr(y1-y2);
  71. }
  72. i64 toInt64(string s) {
  73.     i64 r=0;
  74.     istringstream sin(s);
  75.     sin>>r;
  76.     return r;
  77. }
  78. double Log(i64 N, i64 B) {
  79.     return (log10l(N)) / (log10l(B));
  80. }
  81. string itoa(long long a) {
  82.     if(a==0) return "0";
  83.     string ret;
  84.     for(long long i=a; i>0; i=i/10)
  85.         ret.push_back((i%10)+48);
  86.     reverse(ret.begin(),ret.end());
  87.     return ret;
  88. }
  89. vector< string > token( string a, string b ) {
  90.     const char *q = a.c_str();
  91.     while( count( b.begin(), b.end(), *q ) ) q++;
  92.     vector< string > oot;
  93.     while( *q ) {
  94.         const char *e = q;
  95.         while( *e && !count( b.begin(), b.end(), *e ) )
  96.             e++;
  97.         oot.push_back( string( q, e ) );
  98.         q = e;
  99.         while( count( b.begin(), b.end(), *q ) )
  100.             q++;
  101.     }
  102.     return oot;
  103. }
  104.  
  105. int isvowel(char s) {
  106.     s=tolower(s);
  107.     if(s=='a' or s=='e' or s=='i' or s=='o' or s=='u')
  108.         return 1;
  109.     return 0;
  110. }
  111. int isupper(char s) {
  112.     if(s>='A' and s<='Z') return 1;
  113.     return 0;
  114. }
  115. template<class T> struct Fraction {
  116.     T a,b;
  117.     Fraction(T a=0,T b=1);
  118.     string
  119.     toString();
  120. };//NOTES:Fraction
  121. template<class T> Fraction<T>::Fraction(T a,T b) {
  122.     T d=gcd(a,b);
  123.     a/=d;
  124.     b/=d;
  125.     if (b<0) a=-a, b=-b;
  126.     this->a=a;
  127.     this->b=b;
  128. }
  129. template<class T> string Fraction<T>::toString() {
  130.     ostringstream
  131.     sout;
  132.     sout<<a<<"/"<<b;
  133.     return sout.str();
  134. }
  135. template<class T> Fraction<T> operator+(Fraction<T> p,Fraction<T> q) {
  136.     return
  137.         Fraction<T>(p.a*q.b+q.a*p.b,p.b*q.b);
  138. }
  139. template<class T> Fraction<T> operator-(Fraction<T> p,Fraction<T> q) {
  140.     return
  141.         Fraction<T>(p.a*q.b-q.a*p.b,p.b*q.b);
  142. }
  143. template<class T> Fraction<T> operator*(Fraction<T> p,Fraction<T> q) {
  144.     return
  145.         Fraction<T>(p.a*q.a,p.b*q.b);
  146. }
  147. template<class T> Fraction<T> operator/(Fraction<T> p,Fraction<T> q) {
  148.     return
  149.         Fraction<T>(p.a*q.b,p.b*q.a);
  150. }
  151.  
  152. /** BitMask **/
  153. int Set(int N, int pos) {
  154.     return N = N | (1 << pos);
  155. }
  156. int Reset(int N,int pos) {
  157.     return N = N & ~(1 << pos);
  158. }
  159. int Check(int N, int pos) {
  160.     return (N & (1 << pos));
  161. }
  162. int toggle(int N, int pos) {
  163.     if( Check(N, pos) )
  164.         return N = Reset(N, pos);
  165.     return N = Set(N, pos);
  166. }
  167.  
  168. const i64 INFFF = 1e16;
  169.  
  170. #define Max 10005
  171. #define Mx 105
  172.  
  173. char X[Max], Z[Mx];
  174. int total, length, n;
  175.  
  176. // i = sequence index, indx = subsequence index
  177. void solve(int indx, int i) {
  178.     if(indx == length) {
  179.         ++total;
  180.         return;
  181.     }
  182.     if( i >= n - (length - 1 - indx) )
  183.         return;
  184.  
  185.     if(X[i] == Z[indx])
  186.         solve(indx + 1, i + 1);
  187.  
  188.     solve(indx, i + 1);
  189. }
  190.  
  191. struct Bigint {
  192.     // representations and structures
  193.     string a; // to store the digits
  194.     int sign; // sign = -1 for negative numbers, sign = 1 otherwise
  195.  
  196.     // constructors
  197.     Bigint() {} // default constructor
  198.     Bigint( string b ) {
  199.         (*this) = b;    // constructor for string
  200.     }
  201.  
  202.     // some helpful methods
  203.     int size() { // returns number of digits
  204.         return a.size();
  205.     }
  206.     Bigint inverseSign() { // changes the sign
  207.         sign *= -1;
  208.         return (*this);
  209.     }
  210.     Bigint normalize( int newSign ) { // removes leading 0, fixes sign
  211.         for( int i = a.size() - 1; i > 0 && a[i] == '0'; i-- )
  212.             a.erase(a.begin() + i);
  213.         sign = ( a.size() == 1 && a[0] == '0' ) ? 1 : newSign;
  214.         return (*this);
  215.     }
  216.  
  217.     // assignment operator
  218.     void operator = ( string b ) { // assigns a string to Bigint
  219.         a = b[0] == '-' ? b.substr(1) : b;
  220.         reverse( a.begin(), a.end() );
  221.         this->normalize( b[0] == '-' ? -1 : 1 );
  222.     }
  223.  
  224.     // conditional operators
  225.     bool operator < ( const Bigint &b ) const { // less than operator
  226.         if( sign != b.sign ) return sign < b.sign;
  227.         if( a.size() != b.a.size() )
  228.             return sign == 1 ? a.size() < b.a.size() : a.size() > b.a.size();
  229.         for( int i = a.size() - 1; i >= 0; i-- ) if( a[i] != b.a[i] )
  230.                 return sign == 1 ? a[i] < b.a[i] : a[i] > b.a[i];
  231.         return false;
  232.     }
  233.     bool operator == ( const Bigint &b ) const { // operator for equality
  234.         return a == b.a && sign == b.sign;
  235.     }
  236.  
  237.     // mathematical operators
  238.     Bigint operator + ( Bigint b ) { // addition operator overloading
  239.         if( sign != b.sign ) return (*this) - b.inverseSign();
  240.         Bigint c;
  241.         for(int i = 0, carry = 0; i<a.size() || i<b.size() || carry; i++ ) {
  242.             carry+=(i<a.size() ? a[i]-48 : 0)+(i<b.a.size() ? b.a[i]-48 : 0);
  243.             c.a += (carry % 10 + 48);
  244.             carry /= 10;
  245.         }
  246.         return c.normalize(sign);
  247.     }
  248.     Bigint operator - ( Bigint b ) { // subtraction operator overloading
  249.         if( sign != b.sign ) return (*this) + b.inverseSign();
  250.         int s = sign;
  251.         sign = b.sign = 1;
  252.         if( (*this) < b ) return ((b - (*this)).inverseSign()).normalize(-s);
  253.         Bigint c;
  254.         for( int i = 0, borrow = 0; i < a.size(); i++ ) {
  255.             borrow = a[i] - borrow - (i < b.size() ? b.a[i] : 48);
  256.             c.a += borrow >= 0 ? borrow + 48 : borrow + 58;
  257.             borrow = borrow >= 0 ? 0 : 1;
  258.         }
  259.         return c.normalize(s);
  260.     }
  261.     Bigint operator * ( Bigint b ) { // multiplication operator overloading
  262.         Bigint c("0");
  263.         for( int i = 0, k = a[i] - 48; i < a.size(); i++, k = a[i] - 48 ) {
  264.             while(k--) c = c + b; // ith digit is k, so, we add k times
  265.             b.a.insert(b.a.begin(), '0'); // multiplied by 10
  266.         }
  267.         return c.normalize(sign * b.sign);
  268.     }
  269.     Bigint operator / ( Bigint b ) { // division operator overloading
  270.         if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
  271.         Bigint c("0"), d;
  272.         for( int j = 0; j < a.size(); j++ ) d.a += "0";
  273.         int dSign = sign * b.sign;
  274.         b.sign = 1;
  275.         for( int i = a.size() - 1; i >= 0; i-- ) {
  276.             c.a.insert( c.a.begin(), '0');
  277.             c = c + a.substr( i, 1 );
  278.             while( !( c < b ) ) c = c - b, d.a[i]++;
  279.         }
  280.         return d.normalize(dSign);
  281.     }
  282.     Bigint operator % ( Bigint b ) { // modulo operator overloading
  283.         if( b.size() == 1 && b.a[0] == '0' ) b.a[0] /= ( b.a[0] - 48 );
  284.         Bigint c("0");
  285.         b.sign = 1;
  286.         for( int i = a.size() - 1; i >= 0; i-- ) {
  287.             c.a.insert( c.a.begin(), '0');
  288.             c = c + a.substr( i, 1 );
  289.             while( !( c < b ) ) c = c - b;
  290.         }
  291.         return c.normalize(sign);
  292.     }
  293.     // output method
  294.     void println() {
  295.         if( sign == -1 ) putchar('-');
  296.         for( int i = a.size() - 1; i >= 0; i-- ) putchar(a[i]);
  297.         putchar('\n');
  298.     }
  299. };
  300.  
  301. Bigint dp[Mx][Max];
  302.  
  303. int main(void) {
  304. #ifndef ONLINE_JUDGE
  305.     READ("input.txt");
  306. #endif
  307.     int tcase, caseNo = 0;
  308.     SDi(tcase);
  309.     getc(stdin);
  310.     while(tcase--) {
  311.         gets(X);
  312.         gets(Z);
  313.         length = strlen(Z);
  314.         n = strlen(X);
  315.         // complete search and got TLE
  316. //        total = 0;
  317. //        solve(0, 0);
  318. //        println(total);
  319.  
  320.  
  321.         Bigint zero = Bigint("0");
  322.         Bigint one  = Bigint("1");
  323.  
  324.         //as a non-empty substring of the subsequence cannot appear in an empty string
  325.         FOR(i, 1, length) dp[i][0] = zero;
  326.  
  327.         // as the null string appears once in any string
  328.         FOR(i, 0, n) dp[0][i] = one;
  329.  
  330.         FOR(i, 1, length)
  331.             FOR(j, 1, n)
  332.                 if(Z[i - 1] == X[j - 1])
  333.                     dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1]; // 1 new match for every occurence of Z1...Zi in X1...Xj
  334.                 else
  335.                     dp[i][j] = dp[i][j - 1]; // No new occurence, so copy previous value
  336.  
  337.         dp[length][n].println();
  338.     }
  339.     return 0;
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement