【CodeFores-918】 A B C【思维】 D【记忆化搜索】

A - Eleven

Eleven wants to choose a new name for herself. As a bunch of geeks, her friends suggested an algorithm to choose a name for her. Eleven wants her name to have exactly n characters.

Her friend suggested that her name should only consist of uppercase and lowercase letters ‘O’. More precisely, they suggested that the i-th letter of her name should be ‘O’ (uppercase) if i is a member of Fibonacci sequence, and ‘o’ (lowercase) otherwise. The letters in the name are numbered from 1 to n. Fibonacci sequence is the sequence f where

f1 = 1,
f2 = 1,
fn = fn - 2 + fn - 1 (n > 2).
As her friends are too young to know what Fibonacci sequence is, they asked you to help Eleven determine her new name.

Input
The first and only line of input contains an integer n (1 ≤ n ≤ 1000).

Output
Print Eleven’s new name on the first and only line of output.

Example
Input
8
Output
OOOoOooO
Input
15
Output
OOOoOooOooooOoo
水题
代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 1e3+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/

int f[N]={0,1,1};
int main(){
    for(int i=3;i<N;i++) f[i]=f[i-1]+f[i-2];
    int n;scanf("%d",&n);
    int j=2;
    for(int i=1;i<=n;i++){
        if(f[j]==i) {
            putchar('O');
            j++;
        }else putchar('o');
    }
    puts("");

return 0;
}

B - Radio Station

As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin’s task was to add comments to nginx configuration for school’s website. The school has n servers. Each server has a name and an ip (names aren’t necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we’ll assume that an nginx command is of form “command ip;” where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers.

Each ip is of form “a.b.c.d” where a, b, c and d are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has m commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is “command ip;” Dustin has to replace it with “command ip; #name” where name is the name of the server with ip equal to ip.

Dustin doesn’t know anything about nginx, so he panicked again and his friends asked you to do his task for him.

Input
The first line of input contains two integers n and m (1 ≤ n, m ≤ 1000).

The next n lines contain the names and ips of the servers. Each line contains a string name, name of the server and a string ip, ip of the server, separated by space (1 ≤ |name| ≤ 10, name only consists of English lowercase letters). It is guaranteed that all ip are distinct.

The next m lines contain the commands in the configuration file. Each line is of form “command ip;” (1 ≤ |command| ≤ 10, command only consists of English lowercase letters). It is guaranteed that ip belongs to one of the n school servers.

Output
Print m lines, the commands in the configuration file after Dustin did his task.

Example
Input
2 2
main 192.168.0.2
replica 192.168.0.1
block 192.168.0.1;
proxy 192.168.0.2;
Output
block 192.168.0.1; #replica
proxy 192.168.0.2; #main
Input
3 5
google 8.8.8.8
codeforces 212.193.33.27
server 138.197.64.57
redirect 138.197.64.57;
block 8.8.8.8;
cf 212.193.33.27;
unblock 8.8.8.8;
check 138.197.64.57;
Output
redirect 138.197.64.57; #server
block 8.8.8.8; #google
cf 212.193.33.27; #codeforces
unblock 8.8.8.8; #google
check 138.197.64.57; #server

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 1e3+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/

map<string,string>mp;
char s[N];
int main(){
    int n,m;cin>>n>>m;string x,y;
    for(int i=1;i<=n;i++){
        cin>>x>>y;
       // cout<<y<<endl;
        mp[y]=x;
    }

    while(m--){
        cin>>x>>s;
        //cout<<y<<endl;
        cout<<x<<" ";cout<<s;
        int len=strlen(s);
        s[len-1]=0;
        string zz=string(s);
        cout<<" #";cout<<mp[zz]<<endl;
    }
return 0;
}

C - The Monster

As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can’t directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him.

Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.

A string consisting only of parentheses (‘(’ and ‘)’) is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally:

Empty string is a correct bracket sequence.
if s is a correct bracket sequence, then (s) is also a correct bracket sequence.
if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks (‘?’) is called pretty if and only if there’s a way to replace each question mark with either ‘(’ or ‘)’ such that the resulting string is a non-empty correct bracket sequence.

Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≤ l ≤ r ≤ |s| and the string slsl + 1… sr is pretty, where si is i-th character of s.

Joyce doesn’t know anything about bracket sequences, so she asked for your help.

Input
The first and only line of input contains string s, consisting only of characters ‘(‘, ‘)’ and ‘?’ (2 ≤ |s| ≤ 5000).

Output
Print the answer to Will’s puzzle in the first and only line of output.

Example
Input
((?))
Output
4
Input
??()??
Output
7
Note
For the first sample testcase, the pretty substrings of s are:

“(?” which can be transformed to “()”.
“?)” which can be transformed to “()”.
“((?)” which can be transformed to “(())”.
“(?))” which can be transformed to “(())”.
For the second sample testcase, the pretty substrings of s are:

“??” which can be transformed to “()”.
“()”.
“??()” which can be transformed to “()()”.
“?()?” which can be transformed to “(())”.
“??” which can be transformed to “()”.
“()??” which can be transformed to “()()”.
“??()??” which can be transformed to “()()()”.
分析:
首先我们一定要简化一下问题:对于只有()括号的一串字符,问是否匹配,你可以怎么弄?
第一:用stack肯定可以,但是大材小用,用栈的话多种括号的也是可以求的。
第二:我们想一下,对于任意一个右括号一定是要有一个左括号来对应才可以,那么我们O(n)遍历一遍,对于每个右括号看是不是都是左括号的个数大于右括号的个数。然后最后判断是否匹配就看最后的 左括号的个数是不是等于右括号的 个数 。
这样看的话,是不是很简单。
所以我们延伸一下,如果有问号的话,我们怎么解决。
我们可以记录 左括号的个数,右括号的个数,问号的个数,我们首先将问号看作右括号,如果左括号不够了,我们再将问号转化为左括号就行了。
详细的看代码吧。

代码

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 5000+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/

char s[N];
int main(){
    scanf("%s",s+1); int n=strlen(s+1);
     int ans=0;
     int l,r,z;

     for(int i=1;i<=n;i++){
        l=r=z=0;
        for(int j=i;j<=n;j++){
            if(s[j]=='(') l++;
            else if(s[j]==')') r++;
            else if(s[j]=='?') z++;

            if(r+z>l ){
                if(z==0)  break;
                else {
                    z--; l++; //  问号转化为左括号
                }
            }

            if(r+z==l && (j-i+1)%2==0 ) {
                 ans++;
                // printf("%d %d \n",i,j);
            }
        }
     }

     printf("%d\n",ans);
return 0;
}

D - MADMAX

As we all know, Max is the best video game player among her friends. Her friends were so jealous of hers, that they created an actual game just to prove that she’s not the best at games. The game is played on a directed acyclic graph (a DAG) with n vertices and m edges. There’s a character written on each edge, a lowercase English letter.

Max and Lucas are playing the game. Max goes first, then Lucas, then Max again and so on. Each player has a marble, initially located at some vertex. Each player in his/her turn should move his/her marble along some edge (a player can move the marble from vertex v to vertex u if there’s an outgoing edge from v to u). If the player moves his/her marble from vertex v to vertex u, the “character” of that round is the character written on the edge from v to u. There’s one additional rule; the ASCII code of character of round i should be greater than or equal to the ASCII code of character of round i - 1 (for i > 1). The rounds are numbered for both players together, i. e. Max goes in odd numbers, Lucas goes in even numbers. The player that can’t make a move loses the game. The marbles may be at the same vertex at the same time.
这里写图片描述
Since the game could take a while and Lucas and Max have to focus on finding Dart, they don’t have time to play. So they asked you, if they both play optimally, who wins the game?

You have to determine the winner of the game for all initial positions of the marbles.

Input
The first line of input contains two integers n and m (2 ≤ n ≤ 100, ).

The next m lines contain the edges. Each line contains two integers v, u and a lowercase English letter c, meaning there’s an edge from v to u written c on it (1 ≤ v, u ≤ n, v ≠ u). There’s at most one edge between any pair of vertices. It is guaranteed that the graph is acyclic.

Output
Print n lines, a string of length n in each one. The j-th character in i-th line should be ‘A’ if Max will win the game in case her marble is initially at vertex i and Lucas’s marble is initially at vertex j, and ‘B’ otherwise.

Example
Input
4 4
1 2 b
1 3 a
2 4 c
3 4 b
Output
BAAA
ABAA
BBBA
BBBB
Input
5 8
5 3 h
1 2 c
3 1 c
3 2 r
5 1 r
4 3 z
5 4 r
5 2 h
Output
BABBB
BBBBB
AABBB
AAABA
AAAAB
Note
Here’s the graph in the first sample test case:

这里写图片描述
这里写图片描述

分析:想了许久,终于a掉。
我的是纯模拟 A和B走的过程 。
网上的题解 好像都是交替走的dfs,晚点在补一下。

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int N = 100+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/
struct Edge{
    int to,val;
    bool operator < ( const Edge &b )const {
        return val>b.val;
    }
};

vector<Edge>ve[N];
int dp[N][N][30]; // dp[i][j][k] A在i点,B在j点,A先手B后手,A可以走的边权值的最小值为k .
int Judge(int u,int v,int low){  // 记忆化搜索因为同一个状态肯定会走好多次。加上记忆化搜索之后最坏 把所有的状态走一遍 也就是 N*N*26, 肯定可以过。
    if(dp[u][v][low]!=-1) return dp[u][v][low];
    int flag=0;
    for(int i=0;i<ve[u].size();i++){
        Edge e=ve[u][i];
        if(e.val<low) break; //因为是 排好序的,最大的都不可以,之后的肯定也不行
        flag=1; // A的必赢态 : 只要有一个A走的边 ,B走任意的边都可以赢 或者 B一个边都走不了。 
        for(int j=0;j<ve[v].size();j++){
            Edge ee=ve[v][j];
            if(ee.val<e.val) break;
            if(Judge(e.to,ee.to,ee.val)==0) flag=0;
        }
        if(flag==1) break; 
    }
    dp[u][v][low]=flag;    
    if(flag==1){ for(int i=low-1;i>=0;i--) dp[u][v][i]=flag; } //当前的low都可以赢,肯定比其更小的值 A也可以走到必赢的边  .
    return dp[u][v][low];
}

int main(){
    //for(int i='a';i<='z';i++) printf("%c %d\n",i,i-'a');
    int n,m; scanf("%d%d",&n,&m);
    memset(dp,-1,sizeof(dp));
    while(m--){
        char c[5]; int a,b;scanf("%d%d%s",&a,&b,c);
        Edge e={b,c[0]-'a'};
        ve[a].push_back(e);
    }
    for(int i=1;i<=n;i++) sort(ve[i].begin(),ve[i].end());

    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(Judge(i,j,0)==1) putchar('A');
            else putchar('B');
        }
        puts("");
    }
return 0;
}

代码二 :分析先手的必胜态和必输态。

#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long

const int N = 100+11;
const int M = 1e6+11;
const int inf =0x3f3f3f3f;
const int mod = 1e9+7;
const int inff = 0x3f3f3f3f3f3f3f3f;

/*-------------------------------------*/

struct Edge {
    int to,val;
    Edge(int to=0,int  val=0) : to(to),val(val) { }
    bool operator < ( const Edge &b ) const {
        return val>b.val;
    }
};
vector<Edge>ve[N];
int dp[N][N][30];
bool Judge(int u,int v,int low){ // u先手,v后手,low为先手能够走的最小的值
    if(dp[u][v][low]!=-1) return dp[u][v][low];
    int flag=0; // 先手的必输态 : 没有边可以走
    for(int i=0;i<ve[u].size();i++){
        Edge e=ve[u][i];
        if(e.val<low) break;
        if(Judge(v,e.to,e.val)==0)  { flag=1; break; }// 先手必赢态:后手为必输态
    }
    dp[u][v][low]=flag;
    return flag;
}
int main(){
    memset(dp,-1,sizeof(dp));
    int n,m; scanf("%d%d",&n,&m);
    while(m--){
        int a,b;char s[5];
        scanf("%d%d%s",&a,&b,s);
        ve[a].push_back(Edge(b,s[0]-'a'));
    }
    for(int i=1;i<=n;i++) sort(ve[i].begin(),ve[i].end());
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            putchar(Judge(i,j,0)?'A':'B');
        }
        puts("");
    }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值