Hihocoder Microsoft Programming Beauty 2015 Preliminaries second field (violence + dictionary + graph theory + thinking algorithm)

Source: Internet
Author: User

Title 1: Poker Time limit:2000msSingle Point time limit:1000msMemory Limit:256MBDescribe

A deck of poker without a king consists of 52 cards, consisting of red peach, spades, plum, and 4 sets of cards, each set of 13 different denominations. Now given the number of cards in the 52 card, calculate the number of scenarios in which you would rank them in a single column, with different face cards.

The card is represented by XY, where x is the face value, one of 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, a. Y is the suit, one of S, H, D, C. such as 2S, 2H, TD and so on.

Input

The first behavior is an integer t, which is the number of data groups.

Each group of data is then taken in one row. This line first contains an integer n, which represents the number of cards given, followed by a space-delimited string, each string length of 2, representing a card. The cards in each group of data are different.

Output

For each set of data output line, the shape is "case #X: Y". X is the number of data groups, starting from 1. Y is the number of possible scenarios, because the answer can be large, please output the value after modulo 264.

Data range

1≤t≤20000

Small Data

1≤n≤5

Big Data

1≤n≤52

Sample input
Wuyi TC2 TC TS5 2C ad ac JC JH4 ac KC QC JC6 ac AD as JC JD KD
Sample output
Case #1:1Case #2:0Case #3:48Case #4:24Case #5:120

Algorithm Analysis: This topic is to give you n cards, these cards have their own values and colors.
question: give you n cards, this n can be in a variety of sequences, not two adjacent cards
Number of methods of
the same value.
first look at the data, when the small data n max equals 5. 5! =120, violence is perfectly possible!
we read n strings, and each string stored in a one-dimensional subscript of a two-bit array is its label.
For example: s[0][]: TC corresponds to 0
s[1][]: TS correspondence 1
        ......
so we generate: 0 1 2 ... n-1, we'll make a dictionary order of the entire sequence to see
The sequence
of the current sequence corresponds to a series of strings there are two cards that are adjacent if there is no current arrangement
is feasible, cnt++;
Code:
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <    math.h> #include <algorithm> #define N 5010using namespace Std;int num (char *s) {int ans;    if (s[0]>= ' 2 ' && s[0]<= ' 9 ') {ans = s[0]-48;    } else if (s[0]== ' T ') ans = 10;    else if (s[0]== ' J ') ans = 11;    else if (s[0]== ' Q ') ans = 12;    else if (s[0]== ' K ') ans = 13;    else if (s[0]== ' A ') ans = 1; return ans;}    int main () {int t;    scanf ("%d", &t);    int cnt=1;    int n;    int I, j, K;    Char s[10][5];        while (t--) {scanf ("%d", &n);        for (i=0; i<n; i++) {scanf ("%s", S[i]); }//data initialization char STR[10];        memset (str, ' n ', sizeof (str));        for (i=0; i<n; i++) {str[i]=i+48;        } str[n]= ' + ';        int ans=0;            do {int cur;            BOOL Flag=true;           for (i=0; i<n; i++) {if (i==0)         cur = num (s[str[i]-48]);                    else {int dd=num (s[str[i]-48]);                    if (dd = = cur) {flag=false; break;                } else cur = dd;        }} if (Flag==true) ans++;        } while (Next_permutation (str, str+n));    printf ("Case #%d:%d\n", cnt++, ans); } return 0;}

Topic 2 I didn't see it ~ ~ ~

Topic 3: The Xiaoice of gossip Time limit: 2000msSingle Point time limit: 1000msMemory Limit: 256MBDescribe

Xiaoice was a gossip, and recently she was interested in a social networking site.

Since Xiaoice is a robot, it is certainly possible to quickly figure out the user's information in this social networking site.

She found that there were n users in the social networking site, and that users and users could interact with each other. Xiaoice determine the degree of intimacy between each user based on the number and content of interactions between users. A non-negative intimacy, if greater than 0, indicates that the two users are friend relationships. Since this site is active, Xiaoice will constantly update the intimacy between users.

Due to privacy protection, Xiaoice cannot know the exact gender of each user, but as a clever AI, Xiaoice can guess the gender through the behavior of each user. Of course, this speculation is inaccurate, Xiaoice may change the judgment of a user.

Xiaoice want to know how much gossip this social network is. The definition of gossip is the sum of the affinity between all heterosexual friends in a social network. Can you help her?

Input

The first line is an integer t that represents the number of data groups. Next is the T-group data, and the format of each group of data is as follows:

The first line is three integers n, M, Q, respectively, the number of users, the initial friend logarithm, the operand.

The second line is the number of n spaces separated, the number of I indicates the gender of user I, expressed in 0 or 1.

The next M-line, three numbers per line x, Y, Z, represents the initial state of intimacy between user X and user y is Z. In addition, the intimacy between users is initially 0.

Next is the Q line, where each line is one of the following three operations:

1. "1 x": Change the gender of user X

2. "2 x y z": Change the degree of intimacy between user X and user y as Z

3. "3": Ask the Gossip degree

Output

For each set of data, first output one line "case #X:" and X is the test data number.

Next, for each query, the output line contains the gossip of the query.

Data range

1≤t≤20

1≤x, Y≤n

0≤z≤100000

Small Data

1≤n, m≤100

1≤q≤1000

Big Data

1≤n, M, q≤100000

Sample input
13 2 80 1 01 2 11 3 131 11 232 2 3 231 23
Sample output
Case #1:1223

Algorithm analysis: Small data time is very simple, the establishment of two-dimensional matrix storage, every time the gossip value of the time the violence of the map, statistical calculation on it.

Code

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <algorithm> #define N 5010using namespace Std;int n, m, q; Three integers n, M, Q, respectively, indicates the number of users, the initial friend logarithm, the operand. /* The first line is three integers n, M, Q, each representing the number of users, the initial friend logarithm, the operand. The second line is the number of n spaces separated, the number of I indicates the gender of user I, expressed in 0 or 1. The next M-line, three numbers per line x, Y, Z, represents the initial state of intimacy between user X and user y is Z. In addition, the intimacy between users is initially 0. Next is the Q line, where each line is one of the following three operations: 1. "1 x": Change the gender of User X 2. "2 x y z": Changes the intimacy between user X and User y as Z3.    "3": Ask the Gossip degree */int map[110][110];int Main () {int t;    scanf ("%d", &t);    int cnt=1;    int sex[110];        while (t--) {memset (map, 0, sizeof (map));        scanf ("%d%d%d", &n, &m, &q);        for (int i=1; i<=n; i++) scanf ("%d", &sex[i]);        int x, y, Z;            for (int i=0; i<m; i++) {scanf ("%d%d%d", &x, &y, &z);            Map[x][y]=z;        Map[y][x]=z;        } printf ("Case #%d:\n", cnt++);            while (q--) {int dd; scanf ("%d", &AMP;DD);                if (dd==1) {scanf ("%d", &x); SEX[X] = sex[x]==0?1:0; Modify Gender} else if (dd==2) {scanf ("%d%d%d", &x, &y, &z);//change the intimacy between user X and User y                For z map[x][y]=z;            Map[y][x]=z;           } else{//Ask gossip degree: the affinity between all heterosexual friends in social networks and long long ans=0;                /* for (int i=1; i<=n; i++) printf ("%d", sex[i]); printf ("\ n");                    */for (int i=1, i<=n; i++) {for (int j=i+1; j<=n; j + +) {if (map[i][j]>0 && (Sex[i]+sex[j]) ==1) {ans = ans +                            MAP[I][J]; map[i][j]=0; map[j][i]=0;            Break Relationship}}} printf ("%lld\n", ans); }}} return 0;}

how to deal with Big data:

Since it is big data, it can be imagined that when the gossip value every time, it is impossible to ask every time to re-calculate the entire graph of the gossip value.

This must be timed out! So our idea is that after the initial construction of the map, it is good to count the whole Tujantu of the beginning of the gossip value, later if you encounter

If asked, the direct output, if encountered to modify the information of the picture will be dynamically modified the entire graph of the gossip value ans.

1. Assuming that the gender of the modified node x is encountered, the gender modification of x affects only the nodes adjacent to it.

Divided into four cases: Ori (x) =0, cur (x) = 1; The original sex was 0, and now it's 1.

Nodes adjacent to X are assumed to be y:sex (y) =0 or sex (y) =1

In other words: Sex[cur (x)] + sex[y] = = (1+0=1) or (1+1=2)

Ori = 1, cur = 0;

Sex[cur (x)] + sex[y] = = (0+0=0) or (0+1=1)

After the addition of the value of 1, indicating that X modified after the two sexes opposite, gossip value ans to add weight (x-y) weight.

The added value is 0 or 2, indicating that X is modified to the opposite sex, the gossip value ans to subtract (x-y) weight.

2. If you modify the weight between x-y nodes to Z, first look at the X-y node before the gossip value (the initial default is 0: indicates none).

If not, add it, and if so, modify it. If both sexes are the same, modifying the weights will not affect the current

The gossip value ans, if x-y sex is different then you have to consider how to change the gossip value ans, this is very simple, specifically not detailed.

Code:

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include < math.h> #include <vector> #include <algorithm> #define N 100000+10using namespace Std;int n, m, q; Three integers n, M, Q, respectively, indicates the number of users, the initial friend logarithm, the operand.    struct node{int to; int W;}    Temp;int Main () {int t;    scanf ("%d", &t);    int cnt=1;    int sex[n];        while (t--) {vector<node>qm[n];        Long Long ans=0;        scanf ("%d%d%d", &n, &m, &q);        for (int i=1; i<=n; i++) scanf ("%d", &sex[i]);        int x, y, Z;            for (int i=0; i<m; i++) {scanf ("%d%d%d", &x, &y, &z);            if (sex[x]+sex[y]==1) ans = ans+z; Temp.to=y;            Temp.w=z;            Qm[x].push_back (temp);            Temp.to=x; Qm[y].push_back (temp);        Build} printf ("Case #%d:\n", cnt++);            while (q--) {int dd;      scanf ("%d", &AMP;DD);      if (dd==1) {scanf ("%d", &x); SEX[X] = sex[x]==0?1:0;                modify gender int len;                Len=qm[x].size (); for (int i=0; i<len; i++) {if (sex[x]+sex[qm[x][i].to]==1) ans = ans + qm[x][i                    ].W;                    else if (sex[x]+sex[qm[x][i].to]==0) ans = ans-qm[x][i].w;                else if (sex[x]+sex[qm[x][i].to]==2) ans = ans-qm[x][i].w;                }} else if (dd==2) {scanf ("%d%d", &x, &y, &z);//Change the degree of intimacy between user X and user y as Z                int ori=0, cur=0;                int Len;                Len=qm[x].size ();                BOOL Flag=false; for (int i=0; i<len; i++) {if (qm[x][i].to = = y) {flag=true                        ; Ori = QM[X][I].W; Save a copy of the original value qm[x][i].w=z;   Break                 }} if (Flag==false) {temp.to=y; temp.w=z;                Qm[x].push_back (temp);                } len=qm[y].size ();                Flag=false; for (int i=0; i<len; i++) {if (qm[y][i].to = = x) {flag=true                        ; Qm[y][i].w=z;                    Break                    }} if (Flag==false) {temp.to=x; temp.w=z;                Qm[y].push_back (temp);                }//Modify cur =z;            if (sex[x]+sex[y]==1) ans = ans + (Cur-ori);            } else{//Ask gossip: the affinity between all heterosexual friends in the social network and printf ("%lld\n", ans); }}} return 0;}

Hihocoder Microsoft Programming Beauty 2015 Preliminaries second field (violence + dictionary + graph theory + thinking algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.