Concatenation of Languages - UVa 10087 hash

Problem A
Concatenation of Languages
Input File: 
Standard Input

Output: Standard Output

 

A language is a set of strings. And the concatenation of two languages is the set of all strings that are formed by concatenating the strings of the second language at the end of the strings of the first language.

 

For example, if we have two language A and B such that:

A = {cat, dog, mouse}

B = {rat, bat}

The concatenation of A and B would be:

C = {catrat, catbat, dograt, dogbat, mouserat, mousebat}

 

Given two languages your task is only to count the number of strings in the concatenation of the two languages.

 

Input
There can be multiple test cases. The first line of the input file contains the number of test cases, T (1≤T≤25). Then T test cases follow. The first line of each test case contains two integers, M and N (M,N<1500), the number of strings in each of the languages. Then the next M lines contain the strings of the first language. The N following lines give you the strings of the second language. You can assume that the strings are formed by lower case letters (‘a’ to ‘z’) only, that they are less than 10 characters long and that each string is presented in one line without any leading or trailing spaces. The strings in the input languages may not be sorted and there will be no duplicate string.

 

Output

For each of the test cases you need to print one line of output. The output for each test case starts with the serial number of the test case, followed by the number of strings in the concatenation of the second language after the first language.

 

Sample Input                               Output for Sample Input

2

3 2

cat

dog

mouse

rat

bat

1 1

abc

cab

Case 1: 6

Case 2: 1

 



题意:让A和B集合中的字符串组合成新的字符串,但是不能有重复。

思路:直接哈希,注本题有坑,某些字符串中间可能有空格。

AC代码如下:

#include<iostream>
#include<string>
#include<cstring>
#include<cstdio>
using namespace std;

int head[225333],next[225333];
string str1[1510],str2[1510],str3[225333];
int num;
int Hash(string s)
{ int v=0;
  for(int i=0;i<s.size();i++)
  { v*=49;
    v+=s[i]-'a'+1;
    v%=23330;
  }
  return v;
}
void insert(int pos)
{ int h=Hash(str3[pos]);
  int u=head[h];
  while(u)
  { if(str3[u]==str3[pos])
    return;
    u=next[u];
  }
  next[pos]=head[h];
  head[h]=pos;
    num++;
}
int main()
{ int t,T;
  scanf("%d",&T);
  for(t=1;t<=T;t++)
  { int m,n,i,j;
    num=1;
    memset(head,0,sizeof(head));
    scanf("%d%d",&m,&n);
    string str;
    getline(cin,str);;
    for(i=0;i<m;i++)
     getline(cin,str1[i]);
    for(i=0;i<n;i++)
     getline(cin,str2[i]);
    for(i=0;i<m;i++)
    { for(j=0;j<n;j++)
      { str3[num]=str1[i]+str2[j];
        insert(num);
      }
    }
        printf("Case %d: %d\n",t,num-1);
    }
}



### AdaLN 与 Cross-Attention 的区别 AdaLN(Adaptive Layer Normalization)是一种通过条件信息动态调整神经网络层输出分布的技术,主要用于生成模型中,如扩散模型。它通过线性变换从条件向量中预测 scale 和 shift 参数,并将这些参数应用于某个层的归一化结果上,从而实现对特征分布的调整。这种机制允许模型在生成过程中根据不同的条件(如时间步、类别标签)动态调整特征表达[^1]。 Cross-Attention 是一种注意力机制,常见于 Transformer 架构中,用于建模两个不同序列之间的交互关系。在 Cross-Attention 中,查询(Query)和键(Key)来自不同的序列,而值(Value)则用于加权聚合。例如,在图像生成任务中,Cross-Attention 可以用于将文本描述与图像特征进行对齐,从而实现文本到图像的生成。Cross-Attention 的核心思想是通过注意力权重动态选择性地关注输入序列中的关键部分,从而增强模型对输入之间依赖关系的建模能力[^3]。 两者的区别主要体现在以下几个方面: 1. **功能目标**: - AdaLN 主要用于调整神经网络层的输出分布,使得模型能够根据外部条件信息动态调整特征表达。 - Cross-Attention 则用于建模两个不同序列之间的相关性,强调的是跨模态或跨序列的信息交互。 2. **操作机制**: - AdaLN 通过预测 scale 和 shift 参数对特征进行仿射变换。 - Cross-Attention 通过计算注意力权重,动态选择性地聚合输入序列中的信息。 3. **应用场景**: - AdaLN 常用于扩散模型、生成对抗网络等生成模型中,作为条件注入机制。 - Cross-Attention 广泛应用于 Transformer、图像生成、图像描述生成等任务中,用于跨模态信息融合。 ### In-Context Conditions 与 Concatenation of Features 的对比分析 **In-Context Conditions** 指的是在模型推理或训练过程中,将外部条件信息(如类别标签、时间步、文本描述)动态地注入到模型的中间层中。这种注入方式通常通过 AdaLN、FiLM(Feature-wise Linear Modulation)等方式实现,允许模型根据不同的条件信息调整当前层的特征表达。这种方式的优势在于条件信息可以影响整个网络的特征提取过程,而不是仅仅在输入层或输出层进行融合,从而提升模型的适应性和生成质量[^1]。 **Concatenation of Features** 是一种更直接的特征融合方式,即将不同来源的特征向量在某个维度上进行拼接。例如,在图像生成任务中,可以将文本编码器输出的文本特征与图像特征在通道维度上进行拼接,从而形成联合特征表示。这种方式的优势在于实现简单、直观,但缺点是条件信息仅在拼接层之后才被使用,可能无法有效影响更深层的特征提取过程[^3]。 两者的对比如下: 1. **信息注入方式**: - In-Context Conditions 通过仿射变换、注意力机制等方式将条件信息注入到网络中间层,影响特征的分布。 - Concatenation of Features 则是将条件信息作为额外的特征通道直接拼接到输入特征中。 2. **模型影响范围**: - In-Context Conditions 可以在整个网络中传播条件信息,影响多个后续层的计算。 - Concatenation of Features 通常只影响拼接层之后的计算,难以对更深层的特征提取过程产生显著影响。 3. **参数效率**: - In-Context Conditions 通常需要额外的参数来预测 scale 和 shift,但这些参数数量相对较小。 - Concatenation of Features 不需要额外参数,但可能会增加后续层的参数规模。 4. **适用场景**: - In-Context Conditions 更适合需要精细控制特征表达的任务,如扩散模型、文本到图像生成。 - Concatenation of Features 更适合结构简单、特征融合需求不高的任务。 ### 示例代码对比 #### AdaLN 示例 ```python import torch import torch.nn as nn class AdaLN(nn.Module): def __init__(self, feature_dim, condition_dim): super(AdaLN, self).__init__() self.linear = nn.Linear(condition_dim, 2 * feature_dim) def forward(self, x, condition): batch_size, channels = x.shape[0], x.shape[1] parameters = self.linear(condition) scale, shift = parameters.chunk(2, dim=1) scale = scale.view(batch_size, channels, 1, 1) shift = shift.view(batch_size, channels, 1, 1) x = nn.functional.layer_norm(x, x.shape[1:]) * (1 + scale) + shift return x ``` #### Cross-Attention 示例 ```python import torch import torch.nn as nn class CrossAttention(nn.Module): def __init__(self, dim, num_heads=8): super(CrossAttention, self).__init__() self.num_heads = num_heads self.dim = dim self.head_dim = dim // num_heads self.scale = self.head_dim ** -0.5 self.q_proj = nn.Linear(dim, dim) self.k_proj = nn.Linear(dim, dim) self.v_proj = nn.Linear(dim, dim) self.out_proj = nn.Linear(dim, dim) def forward(self, x, context): B, N, C = x.shape B, M, C = context.shape q = self.q_proj(x).reshape(B, N, self.num_heads, self.head_dim).permute(0, 2, 1, 3) k = self.k_proj(context).reshape(B, M, self.num_heads, self.head_dim).permute(0, 2, 1, 3) v = self.v_proj(context).reshape(B, M, self.num_heads, self.head_dim).permute(0, 2, 1, 3) attn = (q @ k.transpose(-2, -1)) * self.scale attn = attn.softmax(dim=-1) x = (attn @ v).transpose(1, 2).reshape(B, N, C) x = self.out_proj(x) return x ``` #### 特征拼接示例 ```python import torch def feature_concat(x, condition): # x: [B, C1, H, W] # condition: [B, C2] # 扩展 condition 到与 x 相同的空间维度 condition = condition.unsqueeze(-1).unsqueeze(-1).expand_as(x[:, :, :1, :1]) # 拼接 x = torch.cat([x, condition], dim=1) return x ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值