Codeforces Round #256 (Div. 2)
水题,把a累加,然后向上取整(double)a/5,把b累加,然后向上取整(double)b/10,然后判断a+b是不是大于n即可
#include <iostream> #include <vector> #include <algorithm> #include <cmath> using namespace std; int main(){ double a1,a2,a3; double b1,b2,b3; int n; cin >>a1 >> a2 >> a3 >>b1 >> b2 >> b3>> n; int cnt = ceil((a1+a2+a3)/5)+ceil((b1+b2+b3)/10); if(cnt > n) cout <<"NO"<<endl; else cout<<"YES"<<endl; }
题目的意思是:
给两个字符串s和t,对字符串s可以进行下面操作(注意每种操作可以进行无限次)
- 删除s的一个字符,如果只做这种操作,输出automation
- 交换s的任意两个字符,如果只做这种操作,输出array
- 如果既要删除又要交换,则输出both
- 如果上面操作无法完成,则输出need tree
解题思路:
(1)首先判断s和t的长度,如果s的长度比t的长度小,则上面的操作都无法完成如果s的长度大于t的长度,则需要删除字符
(2)将s中不属于t内的字符删除,这剩下的字符为str
(3)统计str和t中各自字符串出现的次数,
如果中的字符不在str中,即strCnt.size() < tCnt.size(),则上面操作无法完成。
如果tCnt中某字符的个数大于strCnt的字符的个数,则上面的操作无法完成。
如果strCnt中存在字符的个数大于tcnt的相应字符的个数,则必须要删除s的字符。
如果s的所有字符的个数和t的所有字符的个数相等,则只需要交换即可
(4)最后通过在str中使用两个指针i,index,i指向str,index指向t,遍历str,如果t[index]==str[i],则index++; 最后判断index是否指向t的末尾,如果是,则只需要删除字符即可,否则既要删除又要交换字符。
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <map> using namespace std; int main(){ string s,t; cin >>s >>t; bool needTree = false,automatonFlag = false; if(s.length() < t.length()) needTree = true; else if(s.length() >t.length()) automatonFlag = true; string str=""; for(int i = 0 ; i < s.length(); ++ i ){ if(t.find(s[i])!=string::npos) str+=s[i]; } map<char,int> strCnt,tCnt; for(int i = 0 ; i < str.length(); ++ i) strCnt[str[i]]++; for(int i = 0 ; i < t.length(); ++ i) tCnt[t[i]]++; if(strCnt.size()!=tCnt.size()) needTree = true; else{ for(map<char,int>::iterator iter = tCnt.begin(); iter!=tCnt.end(); iter++){ if(iter->second > strCnt[iter->first]) {needTree = true;break;} if(iter->second < strCnt[iter->first]) automatonFlag = true; } } if(needTree) cout<<"need tree"<<endl; else{ if(!automatonFlag) cout<<"array"<<endl; else{ int i = 0,index = 0; for(int i = 0 ; i < str.length() && index < t.length(); ++i){ if(t[index] == str[i]) index++; } if(index < t.length()) cout<<"both"<<endl; else cout<<"automaton"<<endl; } } }