Summer Camp 1 5

Encryption statement (simulating map)

The simple replacement password is very weak. It encrypts the information composed of one letter by replacing each letter with another letter. Consider the following alternative password description:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
NOPQRSTUVWXYZABCDEFGHIJKLM
Such a description means that when an "A" appears in the input, an "N" should appear in the output. In the same way
, every “B” becomes “O”, and so on, until “Z” becomes “M”. This particular example of an alternative
cipher is called "rot13" (rotation 13-short for rotate-13), and it has an interesting
feature: it is self-decrypting. Encrypting the information again will get the original information.
In such a password, the word "CAT" will become "PNG". And the sentence:
NOW IS THE TIME FOR ALL GOOD PEOPLE TO PROGRAM WELL.
It becomes:
ABJ VF GUR GVZR SBE NYY TBBQ CRBCYR GB CEBTENZ JRYY.
Pay attention to all spaces and punctuation marks so that anything is not in the character set "A"-"Z The characters in "are
unchanged.
Please write a program to implement replacement passwords.

Use map to record the password corresponding to each English letter, and then the corresponding output

#include<bits/stdc++.h>
using namespace std;
map<char,char>q;
string s;
int main()
{
    
    
    getline(cin,s);
    for(int i=0;i<s.size();i++)q['A'+i]=s[i];
 
    getline(cin,s);
    for(int i=0;i<s.size();i++)
    {
    
    
        if(s[i]>='A'&&s[i]<='Z')
        cout<<q[s[i]];
        else cout<<s[i];
    }
    return 0;
}


T2 Aka's large number (greedy + high precision)

Insert picture description here
Considering greed, the question asks to separate two numbers and the largest. Then, first sort the digits from largest to smallest. After sorting, if the ones digit is not 0, then take the ones digit and add it to the largest. If the ones digit is 0 , Find the first one! 0 digits, then add

#include<bits/stdc++.h>
using namespace std;
int n;
int a[15000];
int s1[15000],s2[15000],tep1,tep2;
int q[15000],p[15000],ans[15000];
int gg;
int main()
{
    
    
    cin>>n;
    string s;cin>>s;
    for(int i=1;i<=n;i++)a[i]=s[i-1]-'0';
     
    sort(a+1,a+1+n);
    if(a[1]!=0)
    {
    
    
        for(int i=2;i<=n;i++)q[i-1]=a[i];//取个位
        p[1]=a[1];
    }
    else
    {
    
    
    int tp=1;
    while(!a[tp])tp++;
    int tep=1;
    for(int i=1;i<=n;i++)if(i!=tp)q[tep]=a[i],tep++;
    p[1]=a[tp];//找到第一个!0
    }
 
    for(int i=1;i<=n+1;i++)//高精加
    {
    
    
        ans[i]+=q[i]+p[i];
     
        if(ans[i]>9)
        {
    
    
            ans[i]-=10;
            ans[i+1]++;
        }
    }
    int tep3=n+1;
     
    while(!ans[tep3]&&tep3>1)tep3--;
    for(int i=tep3;i>=1;i--)
    cout<<ans[i];
    return 0;
}


T3 Roasted Music Line (DP)Insert picture description here

70pts O (n * n)
provided f [i] represents the number of programs to the end of the element i, j of the second cycle enumeration from 1 to i j if i is a multiple of, the transfer may be considered
100pts O (nlogn)
Consider All the repeated numbers are merged together to update the dp value of their multiples to become the number of x

#include<bits/stdc++.h>
#define mo 998244353
using namespace std;
long long dp[150000],n,a[150000];
map<int ,int>q;
int main()
{
    
    
    cin>>n;
    for(int i=1;i<=n;i++)
    {
    
    
     cin>>a[i];   
     q[a[i]]++;//记录个数
     dp[i]=1;
    }
    for(int i=1;i<=n;i++)
    {
    
    
        dp[i]=(dp[i]*q[i])%mo;
        for(int j=i*2;j<=n;j+=i)//转移倍数dp值
        dp[j]+=dp[i],dp[j]%=mo;
    }
    long long ans=0;
    for(int i=1;i<=n;i++)ans+=dp[i],ans%=mo;
    cout<<ans+1;
    return 0;
}


T4 Fantasy Road (shortest path)

Insert picture description here
Insert picture description here

Insert picture description here

I’ve already told you the question, this is the shortest question, it should be quite simple.
Just run spfa twice, and for each edge, the result is equal to min(dis(n + 1; x) + dis(n + 2; y), dis(n + 1; y) + dis(n + 2; x ))+val(x,y)
1. Although it is known that it is an undirected graph, there can be many kinds of roads from one city to multiple cities.
2. When recording the edge connecting the fantasy kingdom, it cannot have a certain relationship with the adjacency list. Because the undirected graph is connected twice, the array record needs to be reopened when reading.
3. When initializing, pay attention to n+2 cities. And the new happy planet, the dark planet)

#include<bits/stdc++.h>
#define maxn 150000
using namespace std;
long long dis1[maxn],dis2[maxn],vis[maxn];
int n,m,q,head[maxn],tot;
struct node1
{
    
    
	int xx;
	int yy;
	int zz;
}a[maxn];
struct node
{
    
    
	int next;
	int v;
	int w;
}edge[maxn];
void add(int x,int y,int z)
{
    
    
	tot++;
	edge[tot].next=head[x];
	edge[tot].v=y;
	edge[tot].w=z;
	head[x]=tot;
}

void spfa(int x)
{
    
      
    queue<int>q;
	q.push(x);
	vis[x]=1;
	dis1[x]=0;
	while(!q.empty())
	{
    
    
		int k=q.front();
		q.pop();
		vis[k]=0;
		for(int i=head[k];i;i=edge[i].next)
		{
    
    
			 int neww=edge[i].v;
		    if(dis1[neww]>dis1[k]+edge[i].w)
		    {
    
    
			 dis1[neww]=dis1[k]+edge[i].w;
			 if(!vis[neww])
			 {
    
    
			  q.push(neww);
			  vis[neww]=1;
			 }
		    }
		}
	}
}
void spfa2(int x)
{
    
    
	queue<int>q;
	q.push(x);
	vis[x]=1;
	dis2[x]=0;
	while(!q.empty())
	{
    
    
		int k=q.front();
		q.pop();
		vis[k]=0;
		for(int i=head[k];i;i=edge[i].next)
		{
    
    
		    int neww=edge[i].v;
            if(dis2[neww]>dis2[k]+edge[i].w)
		    {
    
    
			 dis2[neww]=dis2[k]+edge[i].w;
			 if(!vis[neww])
			 {
    
    
			  q.push(neww);
			  vis[neww]=1;
			 }
		    }
		}
	}
}
int main()
{
    
    
	cin>>n>>m>>q;
	for(int i=1;i<=m;i++)
	{
    
    
		int x,y,z;cin>>x>>y>>z;
		a[i].xx=x;a[i].yy=y;a[i].zz=z;
		add(x,y,z);
		add(y,x,z);
	}
	for(int i=1;i<=q;i++)
	{
    
    
		int x,y,z;cin>>x>>y>>z;
		add(x,y,z);
		add(y,x,z);
	}	
	memset(vis,0,sizeof(vis));
    memset(dis1,0x3f3f3f3f,sizeof(dis1));
    memset(dis2,0x3f3f3f3f,sizeof(dis2));
	spfa(n+1);
	memset(vis,0,sizeof(vis));
	spfa2(n+2);	
	/*for(int j=1;j<=n+2;j++)
	cout<<dis1[j]<<' '<<dis2[j]<<endl;*/
	for(int i=1;i<=m;i++)
	{
    
    
//		cout<<dis1[a[i].xx]<<' '<<dis2[a[i].xx]<<' '<<dis1[a[i].yy]<<' '<<dis2[a[i].yy]<<' '<<0x3f3f3f3f <<endl; 
	if(   (   (dis1[a[i].xx]>=0x3f3f3f3f)    ||   (dis2[a[i].yy]>=0x3f3f3f3f)  )  &&   (   (dis2[a[i].xx]>=0x3f3f3f3f)    ||   (dis1[a[i].yy]>=0x3f3f3f3f)  )  ) 
	{
    
    
		cout<<"GG"<<endl;
		continue;
	  }  
	else cout<<min(dis1[a[i].xx]+dis2[a[i].yy]+a[i].zz,dis1[a[i].yy]+dis2[a[i].xx]+a[i].zz)<<endl;
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/yhhy666/article/details/108237548