天津大学2016程序设计题第一题
1、统计数字,字符串匹配:统计“sheep”的数量
先输入字符串个数,再输入各个字符串中单词个数,求各字符串中的sheep个数,区分大小写
输入样例:
输入样例:
3
5
sheep sheeps shep shepr shepp
7
sheep SHEEP Sheep sheep shepr sheer sherp
3
sheep sheep sheep…
样例输出:
case1:this list contains 1 sheep
case2:this list contains 2 sheep
case3:this list contains 2 sheep
超级笨方法,边输入边判断了(这个时间复杂度上机必超时~
输入一个字符串,判断一下它的长度和sheep的长度是否一样,一样了进行比较,不一样就过~
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
cin>>n;
int i=1,flag=0;
while(n--)
{
int m;
cin>>m;
string s;
string a="sheep";
int ans=0;
for(int j=0;j<m;j++)
{
cin>>s;
int l=s.size();
if(l==5)//如果输入的字符串长度超过sheep的长度,就不进行判断
{
for(int k=0;k<5;k++)
if(s[k]!=a[k])
flag=1;
if(!flag)//输入的元素是sheep
ans++;
}
flag=0;
}
cout<<"case"<<i<<" :this list contain "<<ans<<" sheep"<<endl;
i++;
}
}