7-1 Simple Lie Detection (20 分)
作者 陈越
单位 浙江大学
代码长度限制 16 KB
时间限制 400 ms
内存限制 64 MB
Lie detection usually uses a set of prepared questions to ask the testee, and the result is obtained by analyzing the testee’s reactions. The more advanced technology uses a polygraph (测谎仪) to monitor the physical activities of the testee. Our simple polygraph here is to make a judgment by analyzing the characteristics of the answers to the questions.
First, we ask the testee to complete
N
N
N multiple choice questions, each with a single answer. Each question has 8 options, which are represented by the lowercase English letters a
-h
. Then we can obtain a string of length
N
N
N, consisting of only the lowercase English letters a
-h
. Score each string, and those whose score exceeds a certain threshold (阈值)
T
T
T are judged as “suspected liars”. The scoring rules are as follows:
-
the string starts with an
f
has its score − 2 -2 −2; -
the string ends with an
a
has its score − 1 -1 −1; -
for every longest segment of answeres where the same letter is chosen for consecutive questions, if the segment length is larger than 5, the score is to + 3 +3 +3;
-
if an
a
is immediately followed bye
orh
, the score is to − 4 -4 −4; -
for every longest segment of answeres where consecutively increasing letters are chosen to answer consecutive questions (such as
abcd
ordefgh
), if the segment length is larger than 3, the score is to + 5 +5 +5.
Your job is to score the answers and make a judgement.
Input Specification:
Each input file contains one test case. For each case, the first line gives 3 positive integers N N N ( 6 ≤ N ≤ 100 6\le N \le 100 6≤N≤100), the number of questions; T T T ( ≤ 100 \le 100 ≤100), the threshold for judging a liar; and K K K ( ≤ 100 \le 100 ≤100), the number of testees.
Then K K K lines follow, each gives the string of answers of a testee.
Output Specification:
For each testee, print in a line the total score of his/her answers. If the score exceeds the threshold, print !!!
after the score.
Sample Input:
12 1 6
fghaebcdeddd
ahhhhhhgbaaa
cdefffffffff
fffffghecaaa
feeeeeeeegcb
aaaaaabbbbbb
Sample Output:
-1
-2
8!!!
-3
1
6!!!
给出k组询问,每组询问为一个长度为N的字符串:
①字符串以f
开头,则分数-2;
②字符串以a
结尾,则分数-1;
③如果连续相同字母的每个最长的片段长度大于5,则分数+3;
④如果a
后紧跟e
或h
,则分数-4;
⑤如果连续递增的每个最长的片段长度大于3,则分数+5,例如abcd
和defgh
。
每组询问计算对应的分数,如果分数超过阈值K则在分数后输出!!!
#include <bits/stdc++.h>
using namespace std;
int main()
{
int l,t,n;
cin>>l>>t>>n;
while(n--)
{
int r=0,length1=1,length2=1;
string s;
cin>>s;
//字符串以`f`开头,则分数-2;
if(s[0]=='f')r-=2;
//字符串以`a`结尾,则分数-1;
if(s[l-1]=='a')r-=1;
for(int i=1;i<l;i++)
{
if(s[i]==s[i-1])
{ //如果当前字母和上一个字母相同,则length1加1
length1++;
}
else
{ //如果连续相同字母的每个最长的片段长度大于5,则分数+3;
if(length1>5)r+=3;
//初始化length1为1
length1=1;
}
if(s[i]==s[i-1]+1)
{ //如果当前字母比上一个字母大1,则length2加1
length2++;
}
else
{ //如果连续递增的每个最长的片段长度大于3,则分数+5,例如`abcd`和`defgh`。
if(length2>3)r+=5;
//初始化length2为1
length2=1;
}
//如果`a`后紧跟`e`或`h`,则分数-4;
if(s[i-1]=='a'&&(s[i]=='e'||s[i]=='h'))r-=4;
}
//如果连续相同字母的每个最长的片段长度大于5,则分数+3;
if(length1>5)r+=3;
//如果连续递增的每个最长的片段长度大于3,则分数+5,例如`abcd`和`defgh`。
if(length2>3)r+=5;
//每组询问计算对应的分数
cout<<r;
//如果分数超过阈值K则在分数后输出`!!!`
if(r>t)cout<<"!!!";
cout<<endl;
}
return 0;
}
放上紫薇学姐的题解
/*
n道题,阈值为t(分数超过t要输出!!!) ,k个作答
判题规则:
1.字符串首字母为f,分数-2
2.字符串尾字母为a,分数-1
3.每个相同相同字母的len>5的最长答案段,分数+3
即aaaaaaabbbbbbccccccc,分数为9
4.字母a后为e或者h,分数-4
5.连续递增字母且len>3,分数+5
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,t,k;
cin>>n>>t>>k;
while(k--)
{
int res=0;
string s;
cin>>s;
if(s[0]=='f')res-=2;
if(s[n-1]=='a')res-=1;
//for every longest segment of answeres where the same letter is chosen for consecutive questions,
//if the segment length is larger than 5, the score is to +3;
//记录每个字母的最长连续段
int longest_len[200]={0};
for(int i=0;i<n;)
{
int tmp_len=0;
if(i+5<n&&s[i]==s[i+1]&&s[i]==s[i+2]&&s[i]==s[i+3]&&s[i]==s[i+4]&&s[i]==s[i+5])
{
i+=5;
tmp_len+=5;
while(s[i]==s[i+1]&&i+1<n)i++,tmp_len++;
}
else i++;
if(tmp_len>longest_len[s[i-1]])longest_len[s[i-1]]=tmp_len;
}
//验证字母的连续段是否与longest_len相符,是则+5
for(int i=0;i<n;)
{
int tmp_len=0;
if(i+5<n&&s[i]==s[i+1]&&s[i]==s[i+2]&&s[i]==s[i+3]&&s[i]==s[i+4]&&s[i]==s[i+5])
{
i+=5;
tmp_len+=5;
while(s[i]==s[i+1]&&i+1<n)i++,tmp_len++;
}
else i++;
if(tmp_len==longest_len[s[i-1]]&&tmp_len)res+=3;
}
//注意every,测试点1和4会卡
for(int i=0;i+1<n;)
{
if(s[i]=='a'&&s[i+1]=='e'||s[i]=='a'&&s[i+1]=='h')
{
res-=4;
i+=2;
}
else i++;
}
for(int i=0;i<n;)
{
if(i+3<n&&s[i+1]==s[i]+1&&s[i+2]==s[i]+2&&s[i+3]==s[i]+3)
{
res+=5;
i+=3;
while(s[i+1]==s[i]+1&&i+1<n)i++;
}
else i++;
}
if(res>t) cout<<res<<"!!!"<<endl;
else cout<<res<<endl;
}
return 0;
}