iscsi

SRM 178 Div1 L2

Dec 23rd, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. class RandomFA {
  2.     vector<pair<int,int> > parse(string& rule)
  3.     {
  4.         vector<pair<int,int> > ret;
  5.         int ctr=0,a=0,b=0,tmp=0;
  6.         bool state=false;
  7.         while(ctr<rule.size())
  8.         {
  9.             if(rule[ctr]>='0' && rule[ctr]<='9')
  10.             {
  11.                 if(!state)
  12.                 {
  13.                     a*=10;
  14.                     a+=rule[ctr]-'0';
  15.                 }
  16.                 else
  17.                 {
  18.                     b*=10;
  19.                     b+=rule[ctr]-'0';
  20.                 }
  21.             }
  22.             else
  23.             {
  24.                 state=!state;
  25.                 if(!state)
  26.                 {
  27.                     ret.push_back(make_pair(a,b));
  28.                     a=0;b=0;
  29.                 }
  30.             }
  31.             ++ctr;
  32.         }
  33.         ret.push_back(make_pair(a,b));
  34.         return ret;
  35.     }
  36.     vector<vector<pair<int,int> > > parseV(vector <string>& rule)
  37.     {
  38.         int n=rule.size();
  39.         vector<vector<pair<int,int> > > ret;
  40.         REP(i,n)
  41.         {
  42.             ret.push_back(parse(rule[i]));
  43.         }
  44.         return ret;
  45.     }
  46. public:
  47.     double getProbability(vector <string> rulesa, vector <string> rulesb, vector <string> rulesc, int finalState, int maxLength)
  48.     {
  49.         int maxState=-1;
  50.         {
  51.             vector<vector<pair<int,int> > > tmp=parseV(rulesa);
  52.             REP(i,tmp.size())
  53.             REP(j,tmp[i].size())
  54.                 if(tmp[i][j].first>maxState)
  55.                     maxState=tmp[i][j].first;
  56.             tmp=parseV(rulesb);
  57.             REP(i,tmp.size())
  58.             REP(j,tmp[i].size())
  59.                 if(tmp[i][j].first>maxState)
  60.                     maxState=tmp[i][j].first;
  61.             tmp=parseV(rulesc);
  62.             REP(i,tmp.size())
  63.             REP(j,tmp[i].size())
  64.                 if(tmp[i][j].first>maxState)
  65.                     maxState=tmp[i][j].first;
  66.         }
  67.         if(finalState==999)
  68.             finalState=maxState+1;
  69.         vector<vector<vector<int> > > v(maxState+2,vector<vector<int> >(maxState+2,vector<int> (3)));
  70.         {
  71.             vector<vector<pair<int,int> > > tmp=parseV(rulesa);
  72.             REP(i,tmp.size())
  73.             REP(j,tmp[i].size())
  74.                 v[i][tmp[i][j].first][0]=tmp[i][j].second;
  75.             tmp=parseV(rulesb);
  76.             REP(i,tmp.size())
  77.             REP(j,tmp[i].size())
  78.                 v[i][tmp[i][j].first][1]=tmp[i][j].second;
  79.             tmp=parseV(rulesc);
  80.             REP(i,tmp.size())
  81.             REP(j,tmp[i].size())
  82.                 v[i][tmp[i][j].first][2]=tmp[i][j].second;
  83.         }
  84.         REP(i,3)
  85.         REP(k,maxState+1)
  86.         {
  87.             int sum=0;
  88.  
  89.             REP(j,maxState+1)
  90.             {
  91.                 sum+=v[k][j][i];
  92.             }
  93.             v[k][maxState+1][i]=100-sum;
  94.         }
  95.         REP(i,3)
  96.             v[maxState+1][maxState+1][i]=100;
  97.         vector<double> dp(maxState+2);
  98.         dp[0]=1;
  99.         int act=0;
  100.         double ssum=1;
  101.         double sum=1;
  102.         REP(i,maxLength)
  103.         {
  104.             sum*=3;
  105.             ssum+=sum;
  106.         }
  107.         double res=0;
  108.         while(act<maxLength)
  109.         {
  110.             vector<double> dp2(maxState+2);
  111.             res+=dp[finalState];
  112.             REP(i,maxState+2)
  113.             REP(k,maxState+2)
  114.             REP(j,3)
  115.             {
  116.                 dp2[k]+=(dp[i]*(double)v[i][k][j])/100;
  117.             }
  118.             dp.swap(dp2);
  119.             ++act;
  120.         }
  121.         res+=dp[finalState];
  122.         return res/ssum;
  123.     }
  124.  
  125. };
Advertisement
Add Comment
Please, Sign In to add comment