1. 程式人生 > >【STL】【HDU5842】2016中國大學生程序設計競賽 - 網絡選拔賽 K. Lweb and String (set)(水~~~)

【STL】【HDU5842】2016中國大學生程序設計競賽 - 網絡選拔賽 K. Lweb and String (set)(水~~~)

using 試用 字母 個數 urn 答案 def ret cde

鏈接: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)(水~~~)