PAT 1061 Dating [字符串处理]

在一则神秘信息中,Sherlock Holmes迅速解读了隐藏的约会时间。通过寻找字符串中共同的字符,他揭示了周四下午14:04的秘密。此案例展示了字符串处理和逻辑推理的魅力。

Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that those strange strings are actually referring to the coded time Thursday 14:04 -- since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter D, representing the 4th day in a week; the second common character is the 5th capital letter E, representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is s at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time.

Input Specification:

Each input file contains one test case. Each case gives 4 non-empty strings of no more than 60 characters without white space in 4 lines.

Output Specification:

For each test case, print the decoded time in one line, in the format DAY HH:MM, where DAY is a 3-character abbreviation for the days in a week -- that is, MON for Monday, TUE for Tuesday, WED for Wednesday, THU for Thursday, FRI for Friday, SAT for Saturday, and SUN for Sunday. It is guaranteed that the result is unique for each case.

Sample Input:

3485djDkxh4hhGE 
2984akDfkkkkggEdsb 
s&hgsfdk 
d&Hyscvnm

Sample Output:

THU 14:04

代码: 

连题目都看不懂,要怎么和Sherlock约会啊?!!(摔

#include<stdio.h>
#include<string.h>


int main()
{
	char s1[100],s2[100],s3[100],s4[100];
	scanf("%s%s%s%s",s1,s2,s3,s4);
	char week[7][5] = {"MON","TUE","WED","THU","FRI","SAT","SUN"};
	int i,len1 = strlen(s1),len3 = strlen(s3);
	for(i=0;i<len1;i++)
	{
	    //大于小于不能连写 a<=x<=b×
	    //一周只有7天,所以是A-G而不是A-Z 
	    if(s1[i]==s2[i]&&s1[i]<='G'&&s1[i]>='A')
	    {
		  //A-'A'=0,根据题意本应+1表示第一个字母,但数组需要-1,正好抵掉 
		  printf("%s ",week[s1[i]-'A']); 
		  break;
	    }
	}
	//在第一个相同字母后的位置接着搜索 
	for(i++;i<len1;i++)
	{
	    if(s1[i]==s2[i])
	    {
	        if(s1[i]>='0'&&s1[i]<='9')
                {
                    printf("%02d:",s1[i]-'0');
                    break;
                }
	        else if(s1[i]<='N'&&s1[i]>='A')
	        {
		    //A~N表示10-23 
		    printf("%02d:",s1[i]-'A'+10);
	  	    break;
	        }
	    }
	}

	for(i=0;i<len3;i++)
	{
	    // A-Z 65-90 | a-z 97-122
	    if(s3[i]==s4[i]&&(s3[i]>='a'&&s3[i]<='z'||s3[i]>='A'&&s3[i]<='Z'))
	    {
		    //%2d右对齐,%02d右对齐补零 
		    printf("%02d",i);
		    break;
	    }		    
        }
}

 

<think>嗯,用户的问题是关于对Dating数据集进行机器学习预处理,特别是标签项的字符串转数字和第一列的最大最小归一化。首先我需要确认用户提到的Dating数据集具体是哪个,但根据提供的引用内容,引用[4]中有一个例子使用了dating.txt,可能用户指的是这个数据集。不过引用[3]也提到sklearn、kaggle、UCI等数据集来源,但用户具体的数据集可能需要进一步确认。不过用户的问题集中在预处理步骤,所以可能需要根据常规方法来解决。 首先,标签字符串转数字,这在机器学习中很常见,通常使用LabelEncoder或者映射字典的方式。比如,如果标签是类别型的字符串,比如“是”和“否”,或者“高”、“中”、“低”,转换成0、1、2这样的数值,方便模型处理。引用[2]中提到了数据集的划分,但预处理部分可能需要参考特征工程中的方法,比如标准化或归一化。而引用[4]中的例子使用了StandardScaler进行标准化,但用户需要的是最大最小归一化,也就是MinMaxScaler。 对于第一列的归一化,用户要求的是最大最小规范化,也就是将数据缩放到指定的范围,通常是0到1。这时候应该用MinMaxScaler,而不是StandardScaler。需要确保只对第一列进行处理,而不是整个数据集,所以可能需要使用ColumnTransformer或者单独处理该列。 接下来考虑代码实现。可能需要用pandas读取数据,然后分割特征和标签。对于标签转换,可以使用sklearn的LabelEncoder。对于第一列的归一化,用MinMaxScaler,并且只应用于该列。需要注意的是,处理特征时,如果其他列不需要处理,要确保它们不被改变。可能需要将数据分为要处理的部分和不需要处理的部分,或者使用ColumnTransformer来指定处理的列。 用户可能对如何具体实施这些步骤不太清楚,所以步骤需要详细。例如,读取数据、查看数据、分割特征和标签、转换标签、归一化第一列、合并数据等。同时,需要注意测试集的处理,是否应该在fit_transform训练集后,用同样的转换器transform测试集,避免数据泄露。但用户当前的问题可能只是针对整个数据集的预处理,可能还未到划分训练测试的阶段,或者可能在划分之后分别处理?引用[2]中的train_test_split是在划分数据集,所以用户可能在预处理时需要先处理整个数据集,或者分开处理。不过通常建议在划分之后分别对训练集和测试集进行预处理,使用训练集的参数来转换测试集,以避免信息泄露。但用户的问题可能还没到那一步,可能只是整体处理,所以可能需要根据具体情况调整。 另外,需要确认数据集中第一列的数据类型是否为数值型,如果是字符串可能需要先处理,但根据用户的问题,第一列应该已经是数值,只需要归一化。标签列是字符串,需要转换为数字。 可能的代码流程: 1. 导入必要的库,如pandas、sklearn的preprocessing中的LabelEncoder和MinMaxScaler。 2. 读取数据,假设数据格式正确,例如dating.txt,列之间可能是分隔的。 3. 分割特征和标签。假设最后一列是标签,其他是特征。 4. 对标签列应用LabelEncoder的fit_transform。 5. 对第一列应用MinMaxScaler,可能需要先提取第一列,转换后再合并回去。 6. 可能需要将其他特征列保持不变,或者如果有其他处理的话,但用户只提到第一列。 7. 最后合并处理后的特征和标签,或者保存为新的数据集。 需要注意的是,处理特征时,如果使用MinMaxScaler,应该只对第一列进行操作。例如,假设特征数据是前三列,标签是第四列。那么特征中的第一列(索引0)需要归一化,其他列可能不需要处理。此时,可以使用ColumnTransformer来指定只处理第一列,或者单独处理该列然后合并其他列。 例如: features = data.iloc[:, :3] # 前三列是特征 labels = data.iloc[:, 3] # 第四列是标签 然后对features的第一列进行处理: scaler = MinMaxScaler() features_first_col = scaler.fit_transform(features.iloc[:, 0].values.reshape(-1,1)) 然后将处理后的第一列与其他列合并: features_processed = np.concatenate([features_first_col, features.iloc[:, 1:3]], axis=1) 或者使用ColumnTransformer: from sklearn.compose import ColumnTransformer preprocessor = ColumnTransformer( transformers=[ ('minmax', MinMaxScaler(), [0]) ], remainder='passthrough' ) features_processed = preprocessor.fit_transform(features) 这样,第一列会被归一化,其他列保持不变。 同时,标签转换: from sklearn.preprocessing import LabelEncoder le = LabelEncoder() labels_encoded = le.fit_transform(labels) 最后,将处理后的特征和标签合并,或者保存。 用户可能需要具体的代码示例,所以在回答时需要提供详细的步骤和代码,并解释每个步骤的作用。同时,需要提醒用户注意事项,比如在划分训练集和测试集后,应在训练集上fit,然后应用到测试集上,避免数据泄露。但根据用户当前的问题,可能只需要整体处理,所以可以分步骤展示。 另外,引用[4]中的例子使用了StandardScaler,但用户需要的是MinMaxScaler,所以需要调整。同时,引用[4]中的代码示例可能对用户有帮助,可以借鉴其结构,但替换为正确的转换方法。 总结步骤: 1. 加载数据,分割特征和标签。 2. 标签编码:使用LabelEncoder。 3. 特征处理:对第一列使用MinMaxScaler。 4. 合并处理后的数据。 5. 可能的数据集划分(但用户可能还未到这一步,所以可以放在注意事项里)。 在代码中需要确保正确应用转换器,并且处理后的数据正确无误。例如,检查处理后的数据范围是否在0-1之间,标签是否已转换为数字。 可能的问题包括:如何处理多个特征中的某一列,如何避免影响其他列,如何确保转换器的正确使用等。需要详细说明这些点,帮助用户正确实施预处理步骤。</think>以下是针对Dating数据集的机器学习预处理实现步骤: --- ### **一、标签字符串转数字** 1. **加载数据集并分割特征与标签** ```python import pandas as pd from sklearn.preprocessing import LabelEncoder # 假设数据集列结构为:特征列1, 特征列2, 特征列3, 标签列 data = pd.read_csv("dating.txt", sep='\t', header=None) features = data.iloc[:, :-1] # 前三列为特征 labels = data.iloc[:, -1] # 最后一列为字符串标签 ``` 2. **使用LabelEncoder转换标签** ```python le = LabelEncoder() encoded_labels = le.fit_transform(labels) ``` --- ### **二、第一列最大最小归一化** 1. **对指定列应用MinMaxScaler** ```python from sklearn.preprocessing import MinMaxScaler from sklearn.compose import ColumnTransformer # 仅对第一列(索引0)进行归一化,其余列保持不变 preprocessor = ColumnTransformer( transformers=[ ('minmax', MinMaxScaler(), [0]) ], remainder='passthrough' ) normalized_features = preprocessor.fit_transform(features) ``` 2. **合并处理后的数据** ```python processed_data = pd.DataFrame(normalized_features, columns=features.columns) processed_data['label'] = encoded_labels ``` --- ### **三、完整代码示例** ```python import pandas as pd from sklearn.preprocessing import LabelEncoder, MinMaxScaler from sklearn.compose import ColumnTransformer # 1. 加载数据 data = pd.read_csv("dating.txt", sep='\t', header=None) # 2. 分割特征与标签 features = data.iloc[:, :-1] labels = data.iloc[:, -1] # 3. 标签编码 le = LabelEncoder() encoded_labels = le.fit_transform(labels) # 4. 第一列归一化 preprocessor = ColumnTransformer( [('scaler', MinMaxScaler(), [0])], remainder='passthrough' ) normalized_features = preprocessor.fit_transform(features) # 5. 合并处理结果 processed_data = pd.DataFrame(normalized_features, columns=features.columns) processed_data['label'] = encoded_labels # 查看处理后的前5行数据 print(processed_data.head()) ``` --- ### **四、注意事项** 1. **数据集划分顺序** 若需划分训练集和测试集,应**先划分再预处理**,避免数据泄露[^2]。示例: ```python from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(features, labels, test_size=0.2) # 在训练集上拟合转换器,再应用到测试集 preprocessor.fit(x_train) x_train_normalized = preprocessor.transform(x_train) x_test_normalized = preprocessor.transform(x_test) ``` 2. **归一化与标准化的选择** 最大最小归一化对异常值敏感,若数据存在极端值,建议改用标准化(`StandardScaler`)[^4]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值