2016 Multi-University Training Contest 4

22 篇文章 0 订阅
15 篇文章 2 订阅

HDU【5763】——Another Meaning


Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
Problem Description

As is known to all, in many cases, a word has two meanings. Such as “hehe”, which not only means “hehe”, but also means “excuse me”.
Today, ?? is chating with MeiZi online, MeiZi sends a sentence A to ??. ?? is so smart that he knows the word B in the sentence has two meanings. He wants to know how many kinds of meanings MeiZi can express.

Input

The first line of the input gives the number of test cases T; T test cases follow.
Each test case contains two strings A and B, A means the sentence MeiZi sends to ??, B means the word B which has two menaings. string only contains lowercase letters.

Limits
T <= 30
|A| <= 100000
|B| <= |A|

Output

For each test case, output one line containing “Case #x: y” (without quotes) , where x is the test case number (starting from 1) and y is the number of the different meaning of this sentence may be. Since this number may be quite large, you should output the answer modulo 1000000007.

Sample Input

4
hehehe
hehe
woquxizaolehehe
woquxizaole
hehehehe
hehe
owoadiuhzgneninougur
iehiehieh

Sample Output

Case #1: 3
Case #2: 2
Case #3: 5
Case #4: 1

Hint

n the first case, “ hehehe” can have 3 meaings: “*he”, “he*”, “hehehe”.
In the third case, “hehehehe” can have 5 meaings: “*hehe”, “he*he”, “hehe*”, “**”, “hehehehe”.

Author

FZU

题意:给你两个字符串s,t,s本身有一种含义,而t在s中不同的匹配的位置有不同的含义,问s有含义的个数。dp+KMP,首先处理出来t在s中匹配的位置,我们dp[i]为以i结尾的字符串共有的含义,所以对于不匹配的位置dp[i] = dp[i-1],对于匹配的位置有两种选择,匹配和不匹配,所以dp[i] = dp[i-1]+dp[i-size(t)]

#include <bits/stdc++.h>

using namespace std;

const int Max = 110000;

const int Mod =  1000000007;

int Next[Max];

char s[Max],t[Max];

bool vis[Max];

void GetNext()
{
    int len = strlen(s);

    int i = 0,j = -1;

    Next[0] = -1;

    while(i<len)
    {
        if(j == -1 || s[i] == s[j])
        {
            i++;

            j++;

            Next[i] = j;
        }
        else j = Next[j];
    }
}

void PP()
{
    GetNext();

    int len1 = strlen(s);

    int len2 = strlen(t);

    memset(vis,false,sizeof(vis));

    int i = 0 ,j = 0;

    while(i<len1)
    {
        if(j==-1 ||s[i]==t[j])
        {
            i++;

            j++;
        }
        else j = Next[j];

        if(j>=len2)
        {
            vis[i] = true;

            j = Next[j];
        }
    }

}

int dp[Max];
int main()
{
    int T;

    scanf("%d",&T);

    for(int z =1;z<=T;z++)
    {
        scanf("%s",s);

        scanf("%s",t);

        PP();

        memset(dp,0,sizeof(dp));

        dp[0] = 1;

        int len = strlen(s);

        int len2 = strlen(t);

        for(int i = 1;i<=len;i++)
        {
            dp[i] = dp[i-1];

            if(vis[i])
                dp[i] = (dp[i]+dp[i-len2])%Mod;
        }

        printf("Case #%d: %d\n",z,dp[len]);
    }
    return 0;
}

HDU【5768】——Lucky7


Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
Problem Description

When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7 candles when he faced a extremely difficult problem, and always solve it in seven minutes.
?? once wrote an autobiography, which mentioned something about himself. In his book, it said seven is his favorite number and he thinks that a number can be divisible by seven can bring him good luck. On the other hand, ?? abhors some other prime numbers and thinks a number x divided by pi which is one of these prime numbers with a given remainder ai will bring him bad luck. In this case, many of his lucky numbers are sullied because they can be divisible by 7 and also has a remainder of ai when it is divided by the prime number pi.
Now give you a pair of x and y, and N pairs of ai and pi, please find out how many numbers between x and y can bring ?? good luck.

Input

On the first line there is an integer T(T≤20) representing the number of test cases.
Each test case starts with three integers three intergers n, x, y(0<=n<=15 , 0 < x < y < 1018 ) on a line where n is the number of pirmes.
Following on n lines each contains two integers pi, ai where pi is the pirme and ?? abhors the numbers have a remainder of ai when they are divided by pi .
It is guranteed that all the pi are distinct and pi!=7 .
It is also guaranteed that p1p2pn<=1018 and 0 < ai < pi <= 105 for every i∈(1…n).

Output

For each test case, first output “Case #x: “,x=1,2,3…., then output the correct answer on a line.

Sample Input

2
2 1 100
3 2
5 3
0 1 100

Sample Output

Case #1: 7
Case #2: 14

Hint

For Case 1: 7,21,42,49,70,84,91 are the seven numbers.
For Case2: 7,14,21,28,35,42,49,56,63,70,77,84,91,98 are the fourteen numbers.

Author

FZU

问在区间[x,y]中有多少的数字能被7整除,并且对于不存在 x%pi=ai 。一眼题解,可以比赛的时候没有时间敲了,我们用p = 7,a = 0和所给的约束条件组成新的剩余系,用中国剩余搞出使他们组合符合条件的数量,然后容斥搞一发就行了,题解说可能会爆long long ,没有处理也过了

#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <algorithm>

using namespace std;

const int Max  = 20;

typedef long long LL;

LL X[Max],Y[Max],n;

bool vis[Max];

void ExGcd(LL a,LL b,LL &x,LL &y)
{
    if(b == 0)
    {
        x = 1;

        y = 0;
    }
    else
    {
        ExGcd(b,a%b,y,x);

        y -= a/b*x;

    }
}

LL CRT(LL r,LL l)
{
    LL M = 1;

    for(int i = 0;i<=n;i++)
    {
        if(!vis[i]) continue;

        M *= X[i];
    }

    LL an =  0;

    LL x,y;

    for(int i = 0;i<=n;i++)
    {
        if(!vis[i]) continue;

        LL ant = M/X[i];

        ExGcd(ant,X[i],x,y);

        x = (x%X[i]+X[i])%X[i];

        an = (an +(((Y[i]*ant)%M)*x)%M+M)%M;
    }
    return (r+M-an)/M-(l-1+M-an)/M;
}

LL x,y;

int main()
{
    int T,z  = 1;

    cin>>T;

    while(T--)
    {
        cin>>n>>x>>y;

        LL num =0;

        for(int i = 0;i<n;i++)
        {
            cin>>X[i]>>Y[i];
        }

        X[n] = 7;

        Y[n] = 0;

        for(int i = 0;i<(1<<n);i++)
        {
            memset(vis,false,sizeof(vis));

            int st = 0;

            for(int j = 0;j<n;j++)
            {
                if(((1<<j)&i))
                {
                    st ++;
                    vis[j] = true;
                }
            }

            vis[n] = true;

            if(st%2)
            {
                num-=CRT(y,x);
            }
            else num+=CRT(y,x);
        }

        cout<<"Case #"<<z++<<": "<<num<<endl;

    }
    return 0;
}

HDU【5769】Substring

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
Problem Description

?? is practicing his program skill, and now he is given a string, he has to calculate the total number of its distinct substrings.
But ?? thinks that is too easy, he wants to make this problem more interesting.
?? likes a character X very much, so he wants to know the number of distinct substrings which contains at least one X.
However, ?? is unable to solve it, please help him.

Input

The first line of the input gives the number of test cases T;T test cases follow.
Each test case is consist of 2 lines:
First line is a character X, and second line is a string S.
X is a lowercase letter, and S contains lowercase letters(‘a’-‘z’) only.

T<=30
1<=|S|<= 105
The sum of |S| in all the test cases is no more than 700,000.

Output

For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the answer you get for that case.

Sample Input

2
a
abc
b
bbb

Sample Output

Case #1: 3
Case #2: 3

Hint

In first case, all distinct substrings containing at least one a: a, ab, abc.
In second case, all distinct substrings containing at least one b: b, bb, bbb.

Author

FZU

SA的模板题

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int Max = 1e5+100;

int t1[Max],t2[Max],c[Max];

int sa[Max],ra[Max],he[Max];

char str[Max];

int n;

bool cmp(int *r,int a,int b,int l)
{
    return r[a] == r[b] && r[a+l] == r[b+l];
}

void DA(int m)
{
    n++;

    int *x = t1,*y = t2;

    for(int i = 0;i<m;i++) c[i] = 0;

    for(int i =0 ; i<n;i++) c[x[i] = str[i]] ++;

    for(int i = 1;i<m;i++) c[i]+=c[i-1];

    for(int i = n-1;i>=0;i--) sa[--c[x[i]]] = i;

    for(int j  =1;j<=n;j<<=1)
    {
        int p = 0;

        for(int i  =n-j;i<n;i++) y[p++] = i;

        for(int i =0 ;i<n;i++) if(sa[i]>=j) y[p++] = sa[i]-j;

        for(int i = 0;i<m;i++) c[i] =0 ;

        for(int i = 0;i<n;i++) c[x[y[i]]]++;

        for(int i = 1;i<m;i++) c[i]+=c[i-1];

        for(int i = n-1;i>=0;i--) sa[--c[x[y[i]]]] = y[i];

        swap(x,y);

        p =1;x[sa[0]] = 0;

        for(int i = 1;i<n;i++) x[sa[i]] = cmp(y,sa[i-1],sa[i],j)?p-1:p++;

        if(p >n) break;

        m = p;
    }

    int k = 0;

    n--;

    for(int i =1;i<=n;i++) ra[sa[i]] = i;

    for(int i = 0;i<n;i++)
    {
        if(k) k--;

        int j = sa[ra[i]-1];

        while(str[i+k] == str[j+k]) k++;

        he[ra[i]] = k;
    }
}

int pr[Max];

char s[5];

int main()
{

    int T;

    scanf("%d",&T);

    for(int z = 1;z<=T;z++)
    {
        scanf("%s",s);

        scanf("%s",str);

        n = strlen(str);

        int st = n+1;

        for(int i = n;i>=0;i--)
        {
            if(str[i] == s[0]) st = i;

            pr[i] = st;
        }

        DA(130);

        LL ans = 0 ;

        for(int i = 1;i<=n;i++)
        {
            ans +=max(0LL,(LL)(n-sa[i]-(max(he[i],pr[sa[i]]-sa[i]))));

        }

        printf("Case #%d: %lld\n",z,ans);
    }
    return 0;
}

HDU【5772】——String problem

Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
Problem Description

This is a simple problem about string. Now a string S contains only ‘0’-‘9’. ?? wants to select a subsequence from this string. And makes this subsequence score maximum. The subsequence’s score is calculated as follows:

Score= Value – Total_Cost

The calculation of the Cost is as follows:
If the number of characters x in the subsequence is kx, And the two coefficients are ax,bx,The cost of character x calculated as follows:

cost[x]=0,kx=0

cost[x]=ax×(kx1)+bx,kx0

TotalCost=i=19cost[i]

The calculation of the Value is as follows:

Value=0;
for(int i=1;i<=length(substr);++i){
     for(int j=1;j<=length(substr);++j){
          if(i!=j)
              Value+=w[id[i]][id[j]];
     }
}

id[i] is the position of the subsequence’s ith character in the original string,for example,if the original string is “13579”,and the subsubquence is “159”,then the array id ={1,3,5}. The w is a weight matrix.

Input

The first line contains an integer T, denoting the number of the test cases.
For each test case, the first line contains one integers n, the length of a string.
Next line contains the string S.
Next ten lines,each line contains ai,bi,denote the char i’s(0-9) coefficients
Next is a n*n matrix w.
Limits:
T<=20,
0<=n<=100
0<=ai<=bi<=1000
0<=w[i][j]<=50

Output

Each test output one line “Case #x: y” , where x is the case number ,staring from 1. y is the Maximum score.

Sample Input

1
3
135
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
1 2
0 0 3
1 0 0
4 0 0

Sample Output

Case #1: 3

Hint

we can choose “15”,id[]={1,3} then Value=w[1][3]+w[3][1]=7,
Total_Cost=2+2=4,Score=7-4=3

Author

FZU

网络流:最大权闭合子图。
思路如下:
首先将点分为3类
第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分)

第二类:原串中的n个点每个点拆出一个点,第i个点权值为 –a[s[i]] (表示要花费)

第三类:对于10种字符拆出10个点,每个点的权值为 -(b[x]-a[x])

那么我们可以得到一个关系图 ,对于第一类中的点Pij,如果想要选择Pij,你就必须要选中第二类中的点i和j,对于第二类中的点如果你想选中第i个点,其对应的字符s[i],那么就必须选中第三类中s[i] 对应的点,因为每个种类的点第一次选中时花费是b[s[i]],而第二类中花费都是a[s[i]],一定要补上b[s[i]]-a[s[i]],而且只需要补上一次。

得到上面的关系图后然后就是普通的最大权闭合子图问题,直接求解即可。

偷贴发题解

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int INF = 0x3f3f3f3f;

typedef struct node
{
    int v,next;

    int flow;

    node(){}

    node(int _v,int _next,LL _flow):v(_v),next(_next),flow(_flow){}

}No;


int a[15],b[15];

int w[110][110];

int n,num[110];

int s,t;

char str[110];

int Head[11000],top;

No E[111000];

int Sy[11000];

void AddEdge(int u,int v,LL flow)
{
    E[top] = node(v,Head[u],flow);

    Head[u] = top++;

    E[top] = node(u,Head[v],0);

    Head[v] = top++;
}


bool bfs()
{
    memset(Sy,0,sizeof(Sy));

    queue<int>Q;

    Sy[s] = 1;

    Q.push(s);

    while(!Q.empty())
    {
        int u = Q.front(); Q.pop();

        for(int i = Head[u];i !=-1;i = E[i].next)
        {
            int v = E[i].v;

            if(!Sy[v] && E[i].flow>0)
            {
                Sy[v] = Sy[u]+1;

                Q.push(v);
            }
        }
    }

    return Sy[t] !=0;
}

int dfs(int u,int Flow)
{
    if(u == t || Flow ==0) return Flow;

    int f = 0;

    for(int  i = Head[u]; i != -1 ; i = E[i].next)
    {
        int v = E[i].v;

        if(Sy[v] == Sy[u]+1)
        {
            int ans = dfs(v,min(Flow,E[i].flow));

            f+=ans;

            Flow-=ans;

            E[i].flow-=ans;

            E[i^1].flow+=ans;

            if(Flow ==0)
                break;
        }
    }

    return f;
}

int Dinic()
{
    int ans = 0;

    while(bfs())
    {
        ans+=dfs(s,INF);
    }

    return ans;
}

int  Solve()
{
    s = 0; top = 0;

    int End = n+10;

    int sum = 0;

    memset(Head,-1,sizeof(Head));

    for(int i = 1;i<=n;i++)
    {
        for(int j = i+1;j<=n;j++)
        {
            End++;

            AddEdge(s,End,w[i][j]+w[j][i]);

            AddEdge(End,i,INF);

            AddEdge(End,j,INF);

            sum+=(w[i][j] + w[j][i]);
        }
    }

    t = ++End;

    for(int i =1;i<=n;i++)
    {
        AddEdge(i,num[i]+n+1,INF);

        AddEdge(i,t,a[num[i]+1]);
    }

    for(int i = 1;i<=10;i++)
    {
        AddEdge(i+n,t,b[i]-a[i]);
    }

    return sum-Dinic();
}
int main()
{
    int T;

   // freopen("1009.in","r",stdin);

    scanf("%d",&T);

    for(int z = 1;z<=T;z++)
    {
        scanf("%d",&n);

        if(n != 0)
            scanf("%s",str);

        for(int i = 1;i<=10;i++)
        {
            scanf("%d %d",&a[i],&b[i]);
        }

        for(int i = 1;i<=n;i++)
        {
            for(int j = 1;j<=n;j++)
            {
                scanf("%d",&w[i][j]);
            }
        }

        for(int i = 1;i<=n;i++)
            num[i] = str[i-1]-'0';

        if(n == 0) printf("Case #%d: 0\n",z);
        else printf("Case #%d: %d\n",z,Solve());
    }
    return 0;
}

HDU【5773】——The All-purpose Zero


|Time Limit: 2000/1000 MS (Java/Others) |Memory Limit: 65536/65536 K (Java/Others)|

Problem Description

?? gets an sequence S with n intergers(0 < n <= 100000,0<= S[i] <= 1000000).?? has a magic so that he can change 0 to any interger(He does not need to change all 0 to the same interger).?? wants you to help him to find out the length of the longest increasing (strictly) subsequence he can get.

Input

The first line contains an interger T,denoting the number of the test cases.(T <= 10)
For each case,the first line contains an interger n,which is the length of the array s.
The next line contains n intergers separated by a single space, denote each number in S.

Output

For each test case, output one line containing “Case #x: y”(without quotes), where x is the test case number(starting from 1) and y is the length of the longest increasing subsequence he can get.

Sample Input

2
7
2 0 2 1 2 0 5
6
1 2 3 3 0 0

Sample Output

Case #1: 5
Case #2: 5

Hint

In the first case,you can change the second 0 to 3.So the longest increasing subsequence is 0 1 2 3 5.

题意:给你一个序列,求这个序列的LIS,其中0可以变成任何一个数。0可以转化成任意整数,包括负数,显然求LIS时尽量把0都放进去必定是正确的。因此我们可以把0拿出来,对剩下的做O(nlogn)的LIS,统计结果的时候再算上0的数量。为了保证严格递增,我们可以将每个权值S[i]减去i前面0的个数,再做LIS,就能保证结果是严格递增的。

#include <bits/stdc++.h>

using namespace std;

const int Max =101000;

int dp[Max*2];

int Find(int x,int l,int r)
{
    while(l<=r)
    {
        int mid = (l+r)>>1;

        if(dp[mid] == x) return mid;

        if(dp[mid]<x)
            l = mid+1;
        else r =mid-1;
    }

    return l;
}

int main()
{
    int T,n,x;

    scanf("%d",&T);

    for(int z = 1;z<=T;z++)
    {
        scanf("%d",&n);

        int num = 0;

        int l  = Max,r = Max;

        for(int i  =0;i<n;i++)
        {
            scanf("%d",&x);

            if(!x)
            {
                num++;

                dp[--l] = -num;
            }

            x-=num;

            int st = Find(x,l,r-1);

            if(st == r)
            {
                dp[r++] = x;
            }
            else dp[st] = min(dp[st],x);
        }

        printf("Case #%d: %d\n",z,r-l);
    }
    return 0;
}

HDU【5774】——Where Amazing Happens


Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
Problem Description

As the premier men’s professional basketball league in the world, the National Basketball Association (NBA) has witnessed many superstars, legendary teams and precious friendships.

这里写图片描述

Here is a list of every season’s champion from the league’s inception in 1946 to 2015.

Season Champion
2015-16 Cleveland Cavaliers
2014-15 Golden State Warriors
2013-14 San Antonio Spurs
2012-13 Miami Heat
2011-12 Miami Heat
2010-11 Dallas Mavericks
2009-10 L.A. Lakers
2008-09 L.A. Lakers
2007-08 Boston Celtics
2006-07 San Antonio Spurs
2005-06 Miami Heat
2004-05 San Antonio Spurs
2003-04 Detroit Pistons
2002-03 San Antonio Spurs
2001-02 L.A. Lakers
2000-01 L.A. Lakers
1999-00 L.A. Lakers
1998-99 San Antonio Spurs
1997-98 Chicago Bulls
1996-97 Chicago Bulls
1995-96 Chicago Bulls
1994-95 Houston Rockets
1993-94 Houston Rockets
1992-93 Chicago Bulls
1991-92 Chicago Bulls
1990-91 Chicago Bulls
1989-90 Detroit Pistons
1988-89 Detroit Pistons
1987-88 L.A. Lakers
1986-87 L.A. Lakers
1985-86 Boston Celtics
1984-85 L.A. Lakers
1983-84 Boston Celtics
1982-83 Philadelphia 76ers
1981-82 L.A. Lakers
1980-81 Boston Celtics
1979-80 L.A. Lakers
1978-79 Seattle Sonics
1977-78 Washington Bullets
1976-77 Portland Trail Blazers
1975-76 Boston Celtics
1974-75 Golden State Warriors
1973-74 Boston Celtics
1972-73 New York Knicks
1971-72 L.A. Lakers
1970-71 Milwaukee Bucks
1969-70 New York Knicks
1968-69 Boston Celtics
1967-68 Boston Celtics
1966-67 Philadelphia 76ers
1965-66 Boston Celtics
1964-65 Boston Celtics
1963-64 Boston Celtics
1962-63 Boston Celtics
1961-62 Boston Celtics
1960-61 Boston Celtics
1959-60 Boston Celtics
1958-59 Boston Celtics
1957-58 St. Louis Hawks
1956-57 Boston Celtics
1955-56 Philadelphia Warriors
1954-55 Syracuse Nats
1953-54 Minneapolis Lakers
1952-53 Minneapolis Lakers
1951-52 Minneapolis Lakers
1950-51 Rochester Royals
1949-50 Minneapolis Lakers
1948-49 Minneapolis Lakers
1947-48 Baltimore Bullets
1946-47 Philadelphia Warriors

(quoted from http://www.nba.com/history/nba-season-recaps/)

Given the team name, it won’t be difficult for you to count how many times this team(with exactly the same name) has made amazing happen.

Input

The first line gives the number of test cases. Each case contains one string S representing the team to be queried.
T<=30.S consists of English letters, digits, punctuations and spaces. And 1<=length(S)<=30.

Output

For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the times this team has won the championship according to the list above.

Sample Input

2
Cleveland Cavaliers
Oklahoma City Thunder

Sample Output

Case #1: 1
Case #2: 0

Author

FZU

脑残题

#include <bits/stdc++.h>

using namespace std;

char s[][50]={
"Cleveland Cavaliers",
"Golden State Warriors",
"San Antonio Spurs",
"Miami Heat",
"Miami Heat",
"Dallas Mavericks",
"L.A. Lakers",
"L.A. Lakers",
"Boston Celtics",
"San Antonio Spurs",
"Miami Heat",
"San Antonio Spurs",
"Detroit Pistons",
"San Antonio Spurs",
"L.A. Lakers",
"L.A. Lakers",
"L.A. Lakers",
"San Antonio Spurs",
"Chicago Bulls",
"Chicago Bulls",
"Chicago Bulls",
"Houston Rockets",
"Houston Rockets",
"Chicago Bulls",
"Chicago Bulls",
"Chicago Bulls",
"Detroit Pistons",
"Detroit Pistons",
"L.A. Lakers",
"L.A. Lakers",
"Boston Celtics",
"L.A. Lakers",
"Boston Celtics",
"Philadelphia 76ers",
"L.A. Lakers",
"Boston Celtics",
"L.A. Lakers",
"Seattle Sonics",
"Washington Bullets",
"Portland Trail Blazers",
"Boston Celtics",
"Golden State Warriors",
"Boston Celtics",
"New York Knicks",
"L.A. Lakers",
"Milwaukee Bucks",
"New York Knicks",
"Boston Celtics",
"Boston Celtics",
"Philadelphia 76ers",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"Boston Celtics",
"St. Louis Hawks",
"Boston Celtics",
"Philadelphia Warriors",
"Syracuse Nats",
"Minneapolis Lakers",
"Minneapolis Lakers",
"Minneapolis Lakers",
"Rochester Royals",
"Minneapolis Lakers",
"Minneapolis Lakers",
"Baltimore Bullets",
"Philadelphia Warriors"
};

int n;

char str[50];

int main()
{
    scanf("%d\n",&n);

    for(int i = 1;i<=n;i++)
    {
        gets(str);

        int num = 0;

        for(int j = 0;j<70;j++)
        {
            if(!strcmp(s[j],str))
            {
                num++;
            }
        }

        printf("Case #%d: %d\n",i,num);
    }
    return 0;
}

HDU【5776】——Bubble Sort


Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
Problem Description

P is a permutation of the integers from 1 to N(index starting from 1).
Here is the code of Bubble Sort in C++.

for(int i=1;i<=N;++i)
    for(int j=N,t;j>i;—j)
        if(P[j-1] > P[j])
            t=P[j],P[j]=P[j-1],P[j-1]=t;

After the sort, the array is in increasing order. ?? wants to know the absolute values of difference of rightmost place and leftmost place for every number it reached.

Input

The first line of the input gives the number of test cases T; T test cases follow.
Each consists of one line with one integer N, followed by another line with a permutation of the integers from 1 to N, inclusive.

limits
T <= 20
1 <= N <= 100000
N is larger than 10000 in only one case.

Output

For each test case output “Case #x: y1 y2 … yN” (without quotes), where x is the test case number (starting from 1), and yi is the difference of rightmost place and leftmost place of number i.

Sample Input

2
3
3 1 2
3
1 2 3

Sample Output

Case #1: 1 1 2
Case #2: 0 0 0

Hint

In first case, (3, 1, 2) -> (3, 1, 2) -> (1, 3, 2) -> (1, 2, 3)
the leftmost place and rightmost place of 1 is 1 and 2, 2 is 2 and 3, 3 is 1 and 3
In second case, the array has already in increasing order. So the answer of every number is 0.

Author

FZU

题意:给你一个序列,问序列的每一个数字在冒泡中交换的次数。c会与其比他大的数字进行交换,然后一直向右走,统计出每一个点的最左端和最右端的位置。

#include <bits/stdc++.h>

using namespace std;

const int Max = 110000;

int Tr[Max];

int a[Max],l[Max],r[Max];

int n;

int LowBite(int x)
{
    return x&(-x);
}

void Up(int x)
{
    while(x<=n)
    {
        Tr[x]++;

        x+=LowBite(x);
    }
}

int Qu(int x)
{
    int sum =0 ;

    while(x>0)
    {
        sum+=Tr[x];

        x-=LowBite(x);
    }

    return sum;

}

int main()
{
    int T;

    scanf("%d",&T);

    for(int z  =1;z<=T;z++)
    {
        scanf("%d",&n);

        memset(Tr,0,sizeof(Tr));

        for(int i = 1;i<=n;i++)
        {
            scanf("%d",&a[i]);

            r[a[i]] = max(a[i],i);
        }

        for(int i  =1;i<=n;i++)
        {
            l[a[i]] = i-(i-1-Qu(a[i]-1));

            Up(a[i]);
        }

        printf("Case #%d:",z);

        for(int i =1;i<=n;i++)
        {
            printf(" %d",r[i]-l[i]);
        }

        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值