• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 195,  comments - 30,  trackbacks - 0
            Status In/Out TIME Limit MEMORY Limit Submit Times Solved Users JUDGE TYPE
            stdin/stdout 3s 8192K 439 74 Standard

            Io and Ao are playing a word game. They say a word in the dictionary by turns. The word in the dictionary only contains lowercase letters. And the end character of the former said word should be the same as the start character of the current said word. They can start the game from any word in the dictionary. Any word shouldn't be said twice. Now, we define the complexity of the game that is the sum length of all words said in the game. Give you a dictionary, can you tell me the max complexity of this word game?

            Input

            The first line contains a single positive integer n(0 < n <=12). Next n lines are n words in the dictionary. The length of each word will not exceed 100.

            Output

            A single integer represents the complexity of the game.

            Sample Input

            3
            joj
            jlu
            acm
            6
            cctv
            redcode
            lindong
            we
            love
            programming
            3
            daoyuanlee
            come
            on
            

            Sample Output

            6
            11
            10
            

            Problem Source: provided by loon

            #include<iostream>
            #include<cstdlib>
            using namespace std;
            struct S
            {
             string a;
             char begin;
             char end;
             int length;
             }s[13];
             int visited[13];
            int temp; 
            void search(int a,int num,int pre)
             {
              
              for(int i=0;i<num;i++ )
              {
               if(s[i].begin==s[pre].end&&visited[i]==0)
               {
                visited[i]=1;
                search(a+s[i].length,num,i);
                if(a+s[i].length>temp)temp=a+s[i].length;
                visited[i]=0;
               }
              }
             }
              int main()
              {
              freopen("s.txt","r",stdin);
              freopen("key.txt","w",stdout);
               int num;
               while(cin>>num)
               {
                int i;
              temp=0;
              for( i=0;i<num;i++)
              {
               cin>>s[i].a;
               s[i].length=(s[i].a).size();
               s[i].begin=(s[i].a)[0];
               s[i].end=(s[i].a)[s[i].length-1];
               if(s[i].length>temp)
               temp=s[i].length;
                 }
              for(i=0;i<num;i++) 
                 {
               memset(visited,0,sizeof(visited));
               visited[i]=1;
               search(s[i].length,num,i);
              }
              cout<<temp<<endl;
             }

              //system("PAUSE");
              return   0;
              }

            以上代碼超時。完全可以剪枝。
            舉個例子
            abc
            cbd
            dbm
            dbacmdp
            我的程序一直搜啊搜,每次搜完都重新開始。比如在以a開頭后,搜到c,下次再搜索時直接利用c的結(jié)果,這是深搜的特點(diǎn)決定的!!!
            *************************
            這種類似的有序搜索都可以用     *  備忘錄方法*
            **************************
            #include<iostream>
            #include<cstdlib>
            using namespace std;
            int num;
            struct S
            {
             string a;
             char begin;
             char end;
             int length;
             }s[13];
             int visited[13];
            int temp;
            int sum[13]; 
            int search(int pre)//·µ»Ø´ÓpreµãÒÔºóµÄ×ܵÄÖµ
             {
              int j=s[pre].length,k=0;
              for(int i=0;i<num;i++ )
              {
               if(s[i].begin==s[pre].end&&visited[i]==0&&i!=pre)//必須要有I!=pre
               {
                visited[i]=1;
                k=search(i)+s[pre].length;
                if(k>j)j=k;
               }
               else
               {
                if(s[i].begin==s[pre].end&&i!=pre)//必須要有i!=pre
                return sum[i]+s[pre].length;//相當(dāng)于備忘錄,而且無需visited[i]=0;
               }
              }
              sum[pre]=j;
              return j;
             }
              int main()
              {
             freopen("s.txt","r",stdin);
              freopen("key.txt","w",stdout);
               while(cin>>num)
               {
                int i,j;
              temp=0;
              j=0;
              for( i=0;i<num;i++)
              {
               cin>>s[i].a;
               s[i].length=(s[i].a).size();
               s[i].begin=(s[i].a)[0];
               s[i].end=(s[i].a)[s[i].length-1];
               if(s[i].length>temp)
               temp=s[i].length;
                 }
              for(i=0;i<num;i++) 
                 {
               memset(visited,0,sizeof(visited));
                        memset(sum,0,sizeof(sum));
               visited[i]=1;
               j=search(i);
               if(j>temp)
               temp=j;
              }
              cout<<temp<<endl;
             }

              //system("PAUSE");
              return   0;
              }

            因?yàn)镮!=pre又錯了幾下。
            以后debug盡量自己用眼睛看,更省時間!!!!!!!!!

            posted on 2009-07-05 12:33 luis 閱讀(328) 評論(0)  編輯 收藏 引用 所屬分類: 搜索給我啟發(fā)題
            <2009年7月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            常用鏈接

            留言簿(3)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            友情鏈接

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            18岁日韩内射颜射午夜久久成人| 国产精品久久精品| 久久91精品综合国产首页| 99久久婷婷国产一区二区| 久久精品国产91久久麻豆自制| 天天久久狠狠色综合| 久久精品国产精品亜洲毛片| 久久这里都是精品| 久久精品中文无码资源站| 99久久免费只有精品国产| 久久久久久免费视频| 国产成人精品久久免费动漫| 久久AⅤ人妻少妇嫩草影院| 一级做a爰片久久毛片看看| 久久精品99久久香蕉国产色戒| 99久久99这里只有免费的精品| 久久久久国产亚洲AV麻豆| 久久人人爽人人爽人人AV | 蜜臀av性久久久久蜜臀aⅴ麻豆| 久久99国产乱子伦精品免费| 精品熟女少妇aⅴ免费久久| 久久国产高潮流白浆免费观看| 久久嫩草影院免费看夜色| 久久天天躁狠狠躁夜夜躁2O2O | 国产精自产拍久久久久久蜜| 久久亚洲精品国产精品婷婷| 久久久青草久久久青草| 久久亚洲中文字幕精品有坂深雪 | 色99久久久久高潮综合影院| 97久久精品人妻人人搡人人玩| 日日狠狠久久偷偷色综合96蜜桃| 99久久人妻无码精品系列蜜桃| 青青草原综合久久大伊人| 婷婷久久综合九色综合绿巨人| 久久精品国产亚洲Aⅴ香蕉 | 一级a性色生活片久久无| 很黄很污的网站久久mimi色| 免费观看久久精彩视频| 伊人丁香狠狠色综合久久| 久久综合狠狠色综合伊人| 久久99热狠狠色精品一区|