題目鏈接
http://vjudge.net/contest/137242#problem/D
Description
You're addicted to a little game called `remember the melody': you hear some notes, and then you repeat it. In most cases, the longer the melody, the harder to repeat, but it isn't always true. Also, melodies of the same length are usually not equally easy to remember. To find a way to define the remember difficulty of a melody, you invented a statistics-based model:Input
The input contains several test cases. Each case consists of three integers n, m, k (1<=m<=n<=100, 1<=k<=20 ) , the next n lines each contain two strings separated by exactly one space: the game, and whether you successfully repeated it. The first string will contain at least one at most 100 upper case letters `C', `D', `E', `F', `G', `A', `B'. The second string will be either `Yes' or `No' (case sensitive). The last test case is followed by a single zero, which should not be processed.Output
For each test case, print the case number and the most difficult melody. If there is more than one solution, output the lexicographically smallest one. If there is no solution, output the string `No solution'.Sample Input
3 2 3 EEECEG Yes BFCEG No DEBFCEGEEC No 3 2 2 AAA No BBB No CCC Yes 0
Sample Output
Case 1: BFC Case 2: No solution
題意:輸入n,m,k 接下來的n行,每行輸入一個1~100的字符串(只包含大寫字母`C', `D', `E', `F', `G', `A', `B'),然後輸入"Yes" 或者"No" 求其中長為k的最難字符串,最難字符串定義如下:設這個子串在n個串中出現p次,在表示為"Yes"的串中出現q次,在同一個串中不重復計算只算一次,q/p比值越小難度越大,並且要保證p>=m 否則不考慮這個字符串,如果有多個字符串比值相同,輸出p較大的支付串,如果有多個p也相同,輸出字典序較小的字符串;
思路:用兩個集合分別記錄這些長為k的子串在n個串中出現的次數和在"Yes"表示的串中出現的次數;
代碼如下:
#include <iostream> #include <algorithm> #include <stdio.h> #include <cstring> #include <cmath> #include <map> #include <bitset> using namespace std; typedef long long LL; map<string,int>mp1; map<string,int>mp2;///Yes; map<string,int>mp3; map<string,int>::iterator it; int main() { int n,m,k,Case=1; while(scanf("%d",&n)&&n) { scanf("%d%d",&m,&k); mp1.clear(); mp2.clear(); char s1[105],s2[10]; for(int j=0; j<n; j++) { scanf("%s%s",s1,s2); int len=strlen(s1); mp3.clear(); for(int i=len-k; i>=0; i--) { s1[i+k]='\0'; string s=(string)(s1+i); if(mp3[s]==0){ mp3[s]++; mp1[s]++; if(s2[0]=='Y') mp2[s]++; } } } int t1=9999,t2=99; string str=""; for(it=mp1.begin(); it!=mp1.end(); it++) { if(it->second>=m) { int w1=mp2[it->first]; int w2=it->second; int f=w1*t2-w2*t1; if(f<0||f==0&&w2>t2||f==0&&w2==t2&&(str>(it->first))) { str=""; str+=it->first; t1=w1; t2=w2; } } } printf("Case %d: ",Case++); if(t1==9999&&t2==99) puts("No solution"); else cout<<str<<endl; } return 0; }