发帖速度
Time Limit:1000MS Memory Limit:32768K
Description:
手机发帖的速度依赖按键次数。同样一句话,张三按键20次完成,李四按键12次就完成,那么李四发帖速度快于张三。英语单词往往按上第一个字符就跳出整个单词了,于是选中便完成输入,而非单词则必须照着长度打。今给出一张单词表,请你对照计算每句话的最少按键次数,用以评估发帖速度。
Input:
第一行为单词表(字串长度小于2000)。第二行开始跟有若干句子。注意: 1. 句子无标点; 2. 句子含若干单词或非单词; 3. 单词长度大于1; 4. 单词中的大小写不影响单词匹配; 5. 空格和发送(回车)都按一键算。
Output:
对于每个句子,输出一行最少按键次数。
Sample Input:
like chinese everything through exam
I like ACM
_
I am Chinese
You are my everything
It is the go through exam
Sample Output:
9
4
8
14
19
终于AC啦啦啦啦啦
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
string word;
bool f(string m)
{
int start = 1;
for (int i = 0,j=0;;)
{
if (word[i] == ' ')
{
if (m[j] == '\0')return true;
j = 0; start = 1; i++;
}
if (m[j] == word[i]&&start)
{
j++; i++;
}
else
{
j = 0; i++; start = 0;
}
if (word[i] == '\0')
{
if (m[j] == '\0')return true;
break;
}
}
return false;
}
int main()
{
getline(cin,word);
for (int i = 0; word[i] != '\0'; i++)
{
if (word[i] <= 'Z' && word[i] >= 'A')word[i] += 32;//换小写
}
int words=0;
/*for (int i = 0; word[i] != '\0'; i++)
{
if (word[i] == ' ')words++;
}//记录单词数目*/
/*string* a = new string[words];
int i, j, k;
for (i = 0,j=0,k=0; word[i] != '\0'; i++)
{
if (word[i] == ' ') {
a[j][k] = '\0';
k == 0; j++; continue;
}
a[j][k++] = word[i];
}a[j][k] = '\0';
//截取单词*/
string str;
while(getline(cin, str))
{
int sum = 0;
char m[2001];
int i, j;
for (i = 0,j=0;str[i]!='\0';)
{
if (str[i] == ' ') {
sum++;//空格的
m[j] = '\0';
if (f(m))sum+=2;//是单词,+2
else
{
sum += strlen(m);//不是单词,+整个长度
}
j = 0; i++;
}
else
{
if (str[i] <= 'Z' && str[i] >= 'A')str[i] += 32;//先换小写
char mm;
mm= str[i++];
m[j++] = mm;
}
}
//最后一个单词
{
m[j] = '\0';
if (f(m))sum+=2;//是单词,+2
else
{
sum += strlen(m);//不是单词,+整个长度
}
}
cout << sum + 1<< endl;//输出结尾包含回车
}
//测试m函数
/*while (cin >> str)
{
if (f(str))cout << 1 << endl;
else cout << 0 << endl;
}*/
}