• Volume 1. Sorting/Searching(uva)


    340 - Master-Mind Hints

    /*读了老半天才把题读懂,读懂了题输出格式没注意,结果re了两次。
    
    题意:先给一串数字S,然后每次给出对应相同数目的的一串数字Si,然后优先统计Si和S对应位相同并且相等的个数L,在统计不在对应位上但相等的的个数R.
    
    当Si全0时,表示结束。
    
    每个数只能用一次。
    
    例如:有
    
    S  1 3 5 5
    S1 1 1 2 3
    S2 4 3 3 5
    Si . . . .
       0 0 0 0
    
    对于S1:则第一步只有第一个对应相等,所以L = 1
    就还剩下3 5 5
        1 2 3,在统计R,下面只有3和上面的3相同,所以R = 1;
    对于S2:L = 2,第二个位置和第四个位置对应相等。
    就还剩下1 5
           4 3,所以R = 0;依次类推。
    */
    #include <iostream>
    #include <fstream>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #define N 1005
    using namespace std;
    
    int n;
    int a[N], b[N], c[N];
    int main()
    {
        int i, j, t = 1, l, r;
        while (cin>>n, n)
        {
            cout<<"Game "<<t++<<':'<<endl;
            for (i = 0; i < n; i++)
                cin>>a[i];
            while (true)
            {
                for (i = 0; i < n; i++)
                    c[i] = a[i];
                l = r = 0;
                for (i = 0; i < n; i++)
                {
                    cin>>b[i];
                    if (b[i] == c[i])/**< 统计对应位相等l的个数 */
                    {
                        l++;
                        c[i] = 0;/**< 置相等位0,统计r时不影响 */
                        b[i] = -1;
                    }
                }
                if (!b[0])
                    break;
                for (i = 0; i < n; i++)
                {
                    if (b[i] == -1)
                        continue;
                    for (j = 0; j < n; j++)
                    {
                        if (b[i] == c[j])/**< 统计r的个数 */
                        {
                            r++;
                            c[j] = 0;
                            break;
                        }
                    }
                }
                cout<<"    "<<'('<<l<<','<<r<<')'<<endl;
            }
        }
        return 0;
    }
    View Code

    10420 - List of Conquests

      1 /*题意:统计每个国家有多少个人被Giovanni喜欢。
      2 
      3 并按字典序输出。
      4 */
      5 
      6 #include <iostream>
      7 #include <string>
      8 #include <cstring>
      9 #include <cstdio>
     10 #include <cstdlib>
     11 #include <fstream>
     12 #include <climits>
     13 #define N 2005
     14 using namespace std;
     15 struct node
     16 {
     17     string c;
     18     int sum;
     19     node()
     20     {
     21         c.clear();
     22         sum = 0;
     23     }
     24     bool operator >= (const node &b)
     25     {
     26         return c >= b.c;
     27     }
     28     friend istream& operator >> (istream &in, node &res)
     29     {
     30         in>>res.c;
     31         return in;
     32     }
     33     friend ostream &operator << (ostream &out, const node &res)
     34     {
     35         out<<res.c<<' '<<res.sum<<endl;
     36         return out;
     37     }
     38     bool operator == (const node &b)
     39     {
     40         return c == b.c;
     41     }
     42 
     43     bool operator != (const node &b)
     44     {
     45         return c != b.c;
     46     }
     47 };
     48 
     49 struct li
     50 {
     51     node s[N];
     52     int p;
     53     li()
     54     {
     55         p = 0;
     56     }
     57     friend ostream &operator << (ostream &out, const li &res)
     58     {
     59         for (int i = 0; i < res.p; i++)
     60             out<<res.s[i];
     61     }
     62     void insert(const node &res)
     63     {
     64         if (p)
     65         {
     66             int l = 0, r = p - 1, mid;
     67             while (l <= r)/**< 二分查找第一个比res大或等于的位置 */
     68             {
     69                 mid = (l + r) >> 1;
     70                 if (s[mid] >= res)/**< 如果大于等于,在左区间位置 */
     71                     r = mid - 1;
     72                 else/**< 在右区间 */
     73                     l = mid + 1;
     74             }
     75             if (s[l] != res)/**< 不存在,插入 */
     76             {
     77                 for (r = p++; r > l; r--)
     78                     s[r] = s[r - 1];
     79                 s[l] = res;
     80             }
     81             s[l].sum++;
     82             return;
     83         }
     84         s[p] = res;
     85         s[p++].sum++;
     86     }
     87 };
     88 li a;
     89 int main()
     90 {
     91     int n, i;
     92     cin>>n;
     93     string tt;
     94     node t;
     95     for (i = 0; i < n; i++)
     96     {
     97         cin>>t;
     98         getline(cin, tt);
     99         a.insert(t);
    100     }
    101     cout<<a;
    102     return 0;
    103 }
    View Code

    10474 - Where is the Marble?

    /**< 题意:给一组数据无序,查询给定数在这组数出现的第一个位置(在有序中) */
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <fstream>
    #include <climits>
    #define N 10005
    using namespace std;
    int n, q;
    
    int sum[N], con[N];
    int main()
    {
        int t = 1, i, m;
        while (cin>>n>>q, n || q)
        {
            memset(con, 0, sizeof(con));
            cout<<"CASE# "<<t++<<':'<<endl;
            for (i = 0; i < n; i++)
            {
                cin>>m;
                con[m]++;/**< 计数排序,每个数不超过10000 */
            }
            sum[0] = 0;
            sum[1] = con[0];
            for (i = 2; i < N; i++)
                sum[i] = sum[i - 1] + con[i - 1];
            for (i = 0; i < q; i++)
            {
                cin>>m;
                if (con[m])
                    cout<<m<<" found at "<<sum[m] + 1<<endl;
                else
                    cout<<m<<" not found"<<endl;
            }
        }
        return 0;
    }
    View Code

    152 - Tree's a Crowd

    /**< 题意:(又是读了半天也没读懂,好纠结的英语)
    在三维空间给一些点,计算每个点与其他点的距离,但只取最短距离,并统计最短距离在[0-10)的个数
    比如某个点的最短距离在[1,2)之间,则对1这个位置的个数加1 */
    #include <iostream>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <fstream>
    #include <climits>
    #include <cmath>
    #include <algorithm>
    #include<iomanip>
    #define N 5005
    
    using namespace std;
    struct node
    {
        int x, y, z;
        /*
        bool operator > (const node &b)
        {
            if (x == b.x)
            {
                if (y == b.y)
                    return z > b.z;
                return y > b.y;
            }
            return x > b.x;
        }*/
    }a[N];
    int con[11];
    int dis(const node &a, const node &b)
    {
        int x = a.x - b.x, y = a.y - b.y, z = a.z - b.z;
        return int(sqrt(x * x + y * y + z * z));
    }
    
    int cmp(const void *a, const void *b)/**<  */
    {
        return ((node *)a)->x > ((node *)b)->x ? 1 : -1;
    }
    int main()
    {
        int n = 0, i, j, tm, t;
        while (cin>>a[n].x>>a[n].y>>a[n].z, (a[n].x || a[n].y || a[n].z))
        {
            n++;
        }
        //qsort(a, n, sizeof(a[0]), cmp);
        memset(con, 0, sizeof(con));
        for (i = 0; i < n; i++)
        {
            tm = 10;
            for (j = 0; j < n; j++)
            {
                if (i != j)
                {
                    t = dis(a[i], a[j]);
                    if (t < tm)
                        tm = t;
                }
            }
            con[tm]++;
        }
        for (i = 0; i < 10; i++)
            cout<<setw(4)<<con[i];
        cout<<endl;
        return 0;
    }
    View Code

    299 - Train Swapping

    /**<
    题目啰嗦了半天,就是火车进行从小到大重排序(冒泡排序)交换的次数 
     */
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    #define N 55
    int a[N];
    int main()
    {
        int n, l, i, j, s;
        cin>>n;
        while (n--)
        {
            cin>>l;
            for (i = 0; i < l; i++)
                cin>>a[i];
            s = 0;
            for (i = 0; i < l; i++)
                for (j = l - 1; j > i; j--)
                {
                    if (a[j] < a[j - 1])
                    {
                        swap(a[j], a[j - 1]);
                        s++;
                    }
                }
            cout<<"Optimal train swapping takes "<<s<<" swaps."<<endl;
        }
        return 0;
    }
    View Code

    120 - Stacks of Flapjacks

    /**<  翻转从小到大排序*/
    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    #define N 35
    
    int a[N];
    int main()
    {
        int i, j, n = 0, p, mm, t;
        char ch = 1;
        while (ch != EOF && cin>>a[n])
        {
            ch = cin.get();
            if (ch == ' ')
            {
                n++;
                continue;
            }
            t = ++n;
            cout<<a[0];
            for (i = 1; i < n; i++)
                cout<<' '<<a[i];
            cout<<endl;
            for (i = 0 ; i < n; n--)
            {
                mm = a[i];
                p = 0;
                for (j = 1; j < n; j++)
                {
                    if (mm < a[j])
                    {
                        mm = a[j];
                        p = j;
                    }
                }
                if (p != n - 1)
                {
                    if (p)
                    {
                        reverse(a, a + p + 1);
                        reverse(a, a + n);
                        cout<<t - p<<' '<<t - n + 1<<' ';
                    }
                    else
                    {
                        reverse(a, a + n);
                        cout<<t - n + 1<<' ';
                    }
                }
            }
            cout<<0<<endl;
        }
        return 0;
    }
    View Code

    156 - Ananagrams

    /**<
    给一些单词,去掉相同字母且个数一样不区分大小写,再给剩下的单词进行排序输出
     */
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #define N 1005
    using namespace std;
    struct node
    {
        string str;
        bool flag;
        int tag[26];
        bool operator == (const node &b)
        {
            for (int i = 0; i < 26; i++)
                if (tag[i] != b.tag[i])
                    return false;
            return true;
        }
        bool operator > (const node &b)
        {
            return str > b.str;
        }
        node(const string &s)
        {
            str = s;
            flag = false;
            memset(tag, 0, sizeof(tag));
            for (int i = 0; s[i]; i++)
                tag[(s[i] | 32) - 'a']++;
        }
        node()
        {
            flag = false;
            memset(tag, 0, sizeof(tag));
        }
    }s[N];
    int p = 0;
    
    void insert(const string &v)
    {
        if (!p)
        {
            s[p++] = v;
            return;
        }
        for (int i = 0; i < p; i++)
            if (s[i] == v)
            {
                s[i].flag = true;
                return;
            }
        int l = 0, r = p - 1, mid;
        while (l <= r)
        {
            mid = (l + r) >> 1;
            if (s[mid] > v)
                r = mid - 1;
            else
                l = mid + 1;
        }
        for (r = p++; r > l; r--)
            s[r] = s[r - 1];
        s[l] = v;
    }
    
    string t;
    int main()
    {
        int i;
        char ch;
        cin.get(ch);
        while (ch != '#')
        {
            t.clear();
            while (ch == ' ' || ch == '
    ')
                cin.get(ch);
            if (ch == '#')
                break;
            while (true)
            {
                t += ch;
                cin.get(ch);
                if (ch == ' ' || ch == '
    ')
                    break;
            }
            insert(t);
        }
        for (i = 0; i < p; i++)
            if (!s[i].flag)
                cout<<s[i].str<<endl;
        return 0;
    }
    View Code

    400 - Unix ls

    /**< 
    对输入的字符串进行排序,按格式输出,最后一列的宽度为所有字符串最长宽度,其余列的宽度为最长宽度加2.
    要求输出所用的行最少
     */
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <iomanip>
    
    using namespace std;
    #define N 105
    string s[N];
    int main()
    {
        int n, i, mm, len, j, t, k, kk;
        while (cin>>n)
        {
            mm = 0;
            for (i = 0; i < n; i++)
            {
                cin>>s[i];
                len = s[i].length();
                if (mm < len)
                    mm = len;
            }
            sort(s, s + n);
            len = (60 - mm) / (mm + 2) + 1;
            for (i = 0; i < 60; i++)
                cout<<'-';
            cout<<endl;
            cout<<left;
            t = n / len;/**< 要输出t行len列 */
            if (n % len)
                t++;
            for (i = 0; i < t; i++)
            {
                kk = mm + 2;
                for (j = i, k = 1; j < n; j += t, k++)
                {
                    if (k == len)/**< 最后一列输出宽度为mm */
                        kk = mm;
                    cout<<setw(kk)<<s[j];
                }
                cout<<endl;
            }
        }
        return 0;
    }
    View Code

    123 - Searching Quickly

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <algorithm>
    #include <iomanip>
    #include <set>
    
    using namespace std;
    
    struct snode
    {
        string str;
        int x, y;/**< 字符串位置 */
        bool operator < (const snode &b)const
        {
            if (str == b.str)
            {
                if (x == b.x)
                    return y < b.y;
                return x < b.x;
            }
            return str < b.str;
        }
        snode()
        {
            str.clear();
            x = y = 0;
        }
        snode(string v, int xx, int yy)
        {
            str = v;
            x = xx;
            y = yy;
        }
        friend ostream & operator << (ostream &out, const snode &v)
        {
            out<<v.str<<' '<<v.x<<' '<<v.y;
            return out;
        }
    };
    
    set<string> ks;
    multiset<snode> res;
    string ts[205];
    
    int main()
    {
        int i, j, p;
        string tmp;
        ks.clear();
        res.clear();
        while (cin>>tmp, tmp != "::")
        {
            ks.insert(tmp);
        }
        i = 0;
        cin.get();
        while (getline(cin, ts[i]))
        {
            j = 0;
            while (true)
            {
                p = j;
                tmp.clear();
                while (ts[i][j] && ts[i][j] != ' ')
                {
                    ts[i][j] |= 32;
                    tmp += ts[i][j++];
                }
                if (ks.find(tmp) == ks.end())/**< 不存在 */
                    res.insert(snode(tmp, i, p));
                if (!ts[i][j])
                    break;
                j++;
            }
            i++;
        }
        for (multiset<snode>::iterator it = res.begin(); it != res.end(); it++)
        {
            for (i = 0; i < it->y; i++)
                cout<<ts[it->x][i];
            for (; ts[it->x][i] != ' ' && ts[it->x][i]; i++)/**< 大写 */
                cout<<char(ts[it->x][i] & 95);
            for (; ts[it->x][i]; i++)
                cout<<ts[it->x][i];
            //cout<<*it;
            cout<<endl;
        }
        return 0;
    }
    View Code

    10194 - Football (aka Soccer)

    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <cstring>
    #include <string>
    #include <cctype>
    #include <iomanip>
    #include <cstdlib>
    #include <algorithm>
    #include <set>
    #include <map>
    
    using namespace std;
    
    struct snode
    {
        snode()
        {
            score = in = out = win = con = tie = lose = 0;
            name.clear();
            _name.clear();
        }
        snode(string v)
        {
            score = in = out = win = con = tie = lose = 0;
            name = v;
            _name.clear();
            for (int i = 0; v[i]; i++)
            {
                if (isalnum(v[i]))
                    _name += (v[i] | 32);
                else
                    _name += v[i];
            }
        }
        string name;
        string _name;
        int score;/**< 总得分 */
        int in;/**< 进球 */
        int out;/**< 对方进球 */
        int win;/**< 赢得场数 */
        int con;/**< 参加比赛次数 */
        int tie;/**< 平局次数 */
        int lose;/**< 输的次数 */
        bool operator < (const snode &b)const
        {
            if (score == b.score)
            {
                if (win == b.win)
                {
                    int ta = in - out, tb = b.in - b.out;
                    if (ta == tb)
                    {
                        if (in == b.in)
                        {
                            if (con == b.con)
                            {
                                return _name < b._name;
                            }
                            return con < b.con;
                        }
                        return in > b.in;
                    }
                    return ta > tb;
                }
                return win > b.win;
            }
            return score > b.score;
        }
        friend istream &operator >> (istream &in, snode &v)
        {
            string tmp;
            getline(in, tmp);
            v = tmp;
            return in;
        }
    
        friend ostream &operator << (ostream &out, const snode &v)
        {
            cout<<v.name<<' '<<v.score<<"p, "<<v.con<<"g ("<<v.win<<'-'<<v.tie<<'-'<<v.lose<<"), ";
            cout<<v.in - v.out<<"gd ("<<v.in<<'-'<<v.out<<')';
            return out;
        }
    
        bool operator == (const snode &v)
        {
            return name == v.name;
        }
    }a[35];
    int n, t, g;
    
    int find_str(string v)
    {
        for (int i = 0; i < t; i++)
            if (a[i] == v)
                return i;
        return -1;
    }
    
    void fun(string v)
    {
        int i = -1;
        while (v[++i] != '#');
        string s1(v.begin(), v.begin() + i);
        /**< 数字处 */
        int t1 = v[++i] - '0';
        if (isdigit(v[++i]))/**< 第二个是不是数字 */
            t1 = 10 * t1 + (v[i++] - '0');
        int t2 = v[++i] - '0';
        if (isdigit(v[++i]))/**< 第二个是不是数字 */
            t2 = 10 * t2 + (v[i++] - '0');
        string s2(v.begin() + i + 1, v.end());
        int p1 = find_str(s1), p2 = find_str(s2);
        //cout<<p1<<p2<<endl;
        if (t1 > t2)/**< 1赢 */
        {
            a[p1].win++;
            a[p2].lose++;
            a[p1].score += 3;
        }
        else
            if (t1 == t2)/**< 平 */
            {
                a[p1].tie++;
                a[p2].tie++;
                a[p1].score++;
                a[p2].score++;
            }
            else/**< 2赢 */
            {
                a[p2].win++;
                a[p1].lose++;
                a[p2].score += 3;
            }
            a[p1].con++;
            a[p2].con++;
            a[p1].in += t1;
            a[p2].in += t2;
            a[p1].out += t2;
            a[p2].out += t1;
    }
    
    int main()
    {
        int i, j;
        cin>>n;
        cin.get();
        string st;
        while (n--)
        {
            getline(cin, st);
            cout<<st<<endl;
            cin>>t;
            cin.get();
            for (i = 0; i < t; i++)
                cin>>a[i];
            cin>>g;
            cin.get();
            for (i = 0; i < g; i++)
            {
                getline(cin, st);
                fun(st);
            }
            sort(a, a + t);
            for (i = 0; i < t; i++)
            {
                cout<<i + 1<<") "<<a[i]<<endl;
            }
            if (n)
                cout<<endl;
        }
        return 0;
    }
    View Code

    755 - 487--3279

    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <cstring>
    #include <string>
    #include <cctype>
    #include <iomanip>
    #include <cstdlib>
    #include <algorithm>
    #include <set>
    #include <map>
    
    using namespace std;
    int ls[] =
    {
        2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
        5, 5, 6, 6, 6, 7, 0, 7, 7, 8,
        8, 8, 9, 9, 9, 0
    };
    char get(char ch)
    {
        return ls[ch - 'A'] + '0';
    }
    
    map<string, int> mp;
    int main()
    {
        int t, n, i, tag;
        cin>>t;
        char ch;
        string tmp;
        while (t--)
        {
            mp.clear();
            cin>>n;
            cin.get();
            for (i = 0; i < n; i++)
            {
                tmp.clear();
                while (ch = cin.get(), ch != '
    ')/**< 对输入进行处理换成全数字的字符串 */
                {
                    if (isdigit(ch))
                    {
                        tmp += ch;
                        continue;
                    }
                    if (isalpha(ch))
                    {
                        tmp += get(ch);
                        continue;
                    }
                }
                mp[tmp]++;/**< 对应字符串个数加1 */
            }
            tag = true;
            for (map<string, int>::iterator it = mp.begin(); it != mp.end(); ++it)
            {
                if (it->second > 1)
                {
                    tmp = it->first;
                    tmp.insert(tmp.begin() + 3, '-');
                    cout<<tmp<<' '<<it->second<<endl;
                    tag = false;
                }
            }
            if (tag)
                cout<<"No duplicates."<<endl;
            if (t)
                cout<<endl;
    
        }
        return 0;
    }
    View Code

    10785 - The Mad Numerologist

    #include <iostream>
    #include <fstream>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <string>
    #include <iomanip>
    #include <cctype>
    #include <climits>
    #include <algorithm>
    #include <map>
    #include <set>
    
    using namespace std;
    map<char, int> mp1, mp2;
    char s1[] =
    {
        'J', 'S', 'B', 'K', 'T', 'C', 'L', 'D', 'M', 'V',
        'N', 'W', 'F', 'X', 'G', 'P', 'Y', 'H', 'Q', 'Z',
        'R'
    };
    char s2[] =
    {
        'A' , 'U', 'E', 'O', 'I'
    };
    int n1, n2, p1, p2;
    void fun1()
    {
        while (n1 > 0)
        {
            p1++;
            if (n1 >= 5)
                mp1[s1[p1]] = 5;
            else
                mp1[s1[p1]] = n1;
            n1 -= 5;
        }
    }
    void fun2()
    {
        while (n2 > 0)
        {
            p2++;
            if (n2 >= 21)
                mp2[s2[p2]] = 21;
            else
                mp2[s2[p2]] = n2;
            n2 -= 21;
        }
    }
    void print()
    {
        int i;
        string s1, s2;
        s1.clear();
        s2.clear();
        for (map<char, int>::iterator it = mp1.begin(); it != mp1.end(); ++it)
        {
            for (i = it->second; i > 0; i--)
                s1 += it->first;
        }
        for (map<char, int>::iterator it = mp2.begin(); it != mp2.end(); ++it)
        {
            for (i = it->second; i > 0; i--)
                s2 += it->first;
        }
        i = 0;
        while (s1[i] && s2[i])
        {
            cout<<s2[i]<<s1[i];
            i++;
        }
        if (s2[i])
            cout<<s2[i];
        cout<<endl;
    }
    int main()
    {
        int m, n, t = 1;
        cin>>m;
        while (m--)
        {
            cout<<"Case "<<t++<<": ";
            cin>>n;
            p1 = p2 = -1;
            n1 = n / 2;
            n2 = n1 + (n & 1);
            mp1.clear();
            mp2.clear();
            fun1();
            fun2();
            print();
        }
        return 0;
    }
    View Code
  • 相关阅读:
    【方法1】删除Map中Value反复的记录,而且仅仅保留Key最小的那条记录
    hdu3415 Max Sum of Max-K-sub-sequence 单调队列
    HBase总结(十八)Hbase rowkey设计一
    Makefile生成器,使用C++和Boost实现
    quartz cron表达式在线生成
    NS3网络仿真(6): 总线型网络
    连载:面向对象葵花宝典:思想、技巧与实践(35)
    zoj 2921 Stock(贪心)
    11g RAC 加节点 之 手动加入vip 资源
    [Unity3D]Unity+Android交互教程——让手机&quot;动&quot;起来
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3675491.html
Copyright © 2020-2023  润新知