zoukankan      html  css  js  c++  java
  • 部分题目 ---DP 快速幂模版 dijstra 排序辅助

    Cycling the words:
    Sample Input
    Copy sample input to clipboard
    Please think about it carefully
    Sample Output
    Please think about it carefully
    think about it carefully Please
    about it carefully Please think
    it carefully Please think about
    carefully Please think about it

    代码: #include<iostream> #include<string> using namespace std; int main() { int i,j=0,k,d,p=0,r; string a,b[100],c; getline(cin,a); d=a.size(); for(i=0;i<=d;++i) if(a[i]==' '||i==d) { b[j++]=a.substr(p,i-p); p=i+1; } for(i=0;i<j;++i) { for(r=0;r<j;r++) cout<<b[r]<<" "; cout<<endl; c=b[0]; for(r=0;r<j-1;++r) b[r]=b[r+1]; b[j-1]=c; } return 0; }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
         
         
         
     
    Time Limit: 1sec    Memory Limit:256MB
    Description
    Rewrite Listing 6.12, GradeExam.cpp, to display the students in increasing order of 
    the number of correct answers.
    Suppose the answers for all students are stored in a two-dimensional array. Each row 
    records an student's ten answers with ten columns. For example, the following array 
    stores the answers for 3 students. 
    
                  0   1   2   3   4   5   6   7   8   9   
    Student   0   A   B   A   C   C   D   E   E   A   D   
    Student   1   D   B   D   C   C   D   A   E   A   D   
    Student   2   E   D   D   A   C   B   E   E   A   D   
    
    The key is stored in a one-dimensional array, as follows:
    
                  0   1   2   3   4   5   6   7   8   9   
    Key           D   B   D   C   C   D   A   E   A   D   
    
    
    Input

    The first line is a positive integer t for the number of test cases.
    Each test case contains m+2 lines. The line 1 contains an integer m (0<m<=100) for number of the stuents. Then followed m lines, each line contains 10 integers seperated by blanks, for one student's answers. The last line contains the correct answers.

    Output

    For each test case,output each student's number and the number of correct answers in increasing order of the number of correct answers. Use the format like the sample output.
     

    Sample Input
    Copy sample input to clipboard
    2
    3
    A B A C C D E E A D
    D B D C C D A E A D
    E D D A C B E E A D
    D B D C C D A E A D
    2
    B B E C C D E E A D
    A B D C C D E E A D
    A B D C C D E E B D
    
    Sample Output
    test case 1:
    Student 2: 5
    Student 0: 7
    Student 1: 10
    test case 2:
    Student 0: 7
    Student 1: 9
    

    ///////

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int tc,num;
    char stu[100][11],ans[12];
    int cnt[110],rank[110];
    
    bool cmp(const int& i , const int& j)   {  return  cnt[i]<cnt[j];  }   //核心:辅助排序的数组
    
    int main()
    {   
        cin>>tc;
        for(int th=1;th<=tc;++th)
        {
            cout<<"test case "<<th<<":"<<endl;
            cin>>num;
            for(int i=0;i<num;++i)
                rank[i]=i;
            
            for(int i=0;i<num;++i)
                for(int j=0;j<10;++j)
                    cin>>stu[i][j];
            for(int j=0;j<10;++j)
                    cin>>ans[j];
                
            for(int i=0;i<num;++i)
            {
                cnt[i]=0;
                for(int j=0;j<10;++j)
                    if(ans[j]==stu[i][j]) 
                        cnt[i]++;    // cout<<i<<" & "<<cnt[i]<<endl;
            }
           // for(int i=0;i<num;++i)
            //    cout<<"* "<<rank[i]<<endl;
            sort(rank,rank+num,cmp);
            for(int i=0;i<num;++i)
            {     cout<<"Student "<<rank[i]<<": ";
                cout<<cnt[rank[i]]<<endl;
            }
        }
        return 0;
    }                                 

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    //////////////////////////////////////////////////  。。。。快速幂模版。。。  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2.

    Input

     The input test file will contain multiple test cases. Each test case consists of a single line containing n (where 0 ≤ n ≤ 1,000,000,000) and m(where 0<m<=10000). The end-of-file is denoted by a single line containing the number −1 -1.

    Output

     For each test case, print the value of Fn%m one pre line, omit any leading zeros.

    Sample Input
    Copy sample input to clipboard
    0 10000
    9 10000
    999999999 10000
    1000000000 10000
    -1 -1
    
    Sample Output
    0
    34
    626
    6875
    
    #include<iostream>
    #include<cstring>
    using namespace std;
    struct mat{
        int matri[3][3];
    };
    inline mat MatMat(mat a,mat e,
    int m) { mat t; for(int i=0;i<2;++i) for(int k=0;k<2;++k) { t.matri[i][k]=0; for(int j=0;j<2;++j) t.matri[i][k]=(t.matri[i][k]+a.matri[i][j]*e.matri[j][k])%m; } return t; } //矩阵相乘取模 inline int quick_mod(int q,int m) { mat I,a; a.matri[0][0]=0; a.matri[1][1]=a.matri[0][1]=a.matri[1][0]=1; I.matri[0][0]=I.matri[1][1]=1; I.matri[0][1]=I.matri[1][0]=0; while (q) { if(q&1) I=MatMat(a,I,m); a=MatMat(a,a,m); q>>=1; } return I.matri[1][1]; } //二分快速幂,二进制思想,把11100 a的位数提出, a = a1 *2 + a2 *2*2 + ...
    int main() { int m,n; while(cin>>n>>m && n!=-1) { if(n==0 || n==1) { cout<<n<<endl; continue; } cout<<quick_mod(n-1,m)<<endl; } return 0; } ///////////////////// ytytytyt yeah!! ,。, ) ///////

    图论:dijkstra

    // Problem#: 1031
    // Submission#: 2438546
    // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
    // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
    // All Copyright reserved by Informatic Lab of Sun Yat-sen University
    /* 2013 11 21
     * 卡住:oo只是一个足够大数值,并不是数学上的oo!!太可怕了!!
     * 练习dijskstra,单元最短路,无向图
     * 顺便学习下<map>的用法
     * 虽然把做数学题的时间占据了,但是最严重的是睡眠不足,头疼,千万别感冒!!
     * 可是怎么避免呢。。。
     */
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <map>
    using namespace std;
    
    const int N = 210;
    const int oo = 1000000;
    int a[N][N];//init
    int dist[N];     //distance
    bool go[N]; //if it had been gone(1)
    
    int dijkstra( int b, int e, int n ){ //起点--b--begin,终点--e--end
        memset(go,0,sizeof(go));    
        for( int i = 0 ; i < n ; i++)
            dist[i] = oo;//全部无穷大
    
        dist[b] = 0;         //起点处为0
        for( int i=0 ; i<n ; i++ )
        {   
            int min = oo;
            int v = b;
            for( int j=0 ; j<n ; j++ )
            {
                if(!go[j] && dist[j]<min)//如果没有走过,搜索剩下节点中最短的路
                {       
                    min = dist[j];//cout<<"i="<<i<<"  dist["<<j<<"]="<<dist[j]<<endl;
                    v = j ;//走到这个点啦,等下搜索这个点到其他点的距离
                }
            }//这个for()是为了找到节点v到到别的节点的最短距离
            go[v] = true;//。。。。第一遍 v=b,这里上面的for无效,因为dist全为 oo ,经过下面的赋值后才有用。。。。。。
            for( int j=0 ; j<n ; j++ )//搜索这个点到其他点的距离,小于则替换,dist[]在这里可改变
            {   
                if(!go[j] && dist[v]+a[v][j]<dist[j])//go[]表示已经搜索过最短的啦,当前即为最短
                    dist[j] = dist[v]+a[v][j];   
                /*。。。。。。第一遍是if(0+a[][]<oo)执行,赋值。。。。。。。。
                 *这就是卡到我睡觉时一直在想的地方吗嘛 T~T ×_× 
                */      
            }
        }
        //从b节点出发,每次更新最短距离
        if(go[e])
            return dist[e];  
        else
            return -1;
    }
    
    int main(){
        int c,n,l;
        string b,e;
        cin >> c;
        while( c-- ){
            cin >> n;
            map<string,int> sysu;
            int num = 0;
            for( int i=0 ; i<N ; i++ )
            {
                for( int j=0 ; j<N ; j++ )
                    a[i][j] = (i==j?0:oo);
            }
            for( int i=0 ; i<n ; i++ )
            {
                cin >> b >> e >> l;
                if( !sysu.count(b) ) sysu[b] = num++;  // 如果容器中没有该字符串,加进去
                if( !sysu.count(e) ) sysu[e] = num++;
                a[sysu[b]][sysu[e]] = a[sysu[e]][sysu[b]] = l;
            }
            cin >> b >> e;
            if( b==e )
                cout << 0 << endl;
            else if(!sysu.count(b)||!sysu.count(e)) 
                cout << -1 << endl;
            else 
                cout << dijkstra(sysu[b],sysu[e],num) << endl;
        }
        return 0;
    }                                 
  • 相关阅读:
    CSS中关于BFC的背后原理是什么
    CSS中浏览器是怎样解析CSS选择器的?
    JavaScript中关于 == 和 === 的区别是什么?
    JavaScript中关于继承的实现方式
    Vue双向绑定原理
    Node之TinyPNG图片无限次数压缩
    React的生命周期示意图
    Image-webp探究
    JavaScript中关于事件的循环机制
    vue导航点击切换 1.0
  • 原文地址:https://www.cnblogs.com/tinyork/p/3474088.html
Copyright © 2011-2022 走看看