链接:http://acm.hdu.edu.cn/showproblem.php?pid=5842
水题,可以用来练习STL中的set
题目大意:给你一串字符串,字符串中的某个字母可以替换为一个数字,求最长上升子序列
例如: aabcdef --> 1123456
acdeaa --> 123411
aabcc --> 11233
dacbdda--> 1234112
红色字体为最长上升子序列
所以我们只需要统计有多少种不同的字母便可以得到答案
代码:(set解法)
1 #include <set> 2 #include <cstdio> 3 #include <iostream> 4 ?5 using namespace std; 6 ?7 int main() 8 { 9 ????set<char> s;10 ????int t,ca = 1,cou;11 ????char ch[100010];12 ????cin >> t;13 ????while(t--)14 ????{ ???15 ????????s.clear(); ??// 清空16 ????????cou = 0; ??// 初始化17 ????????scanf("%s",ch);18 ????????int len = strlen(ch);19 ????????for(int i = 0;i < len;i++)20 ????????????s.insert(ch[i]);21 ????????// 迭代器 ?22 ????????set<char>::iterator it;23 ????????for(it = s.begin();it != s.end();it++)24 ????????????cou++; ??// 利用迭代器统计set容器中的字符个数25 26 ????????printf("Case #%d: %d\n",ca++,cou);27 ????}28 ????29 ????return 0;30 }
用map也可以解决这道问题,希望大家可以在用set解决以后尝试用map解决
代码我就不写了
set、map的相关知识都可以在别人的博客中找到,我就不贴出来了
【STL】【HDU5842】2016中国大学生程序设计竞赛 - 网络选拔赛 K. Lweb and String (set)(水~~~)
原文地址:https://www.cnblogs.com/duny31030/p/8836171.html