| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
竟然有空格!!!我晕,这个数据。。。服。。。为这个白贡献一次哇。。In Reply To:Test Posted by:aoxboxcox at 2014-07-01 11:36:12 另外,用了点std::string就500ms,这是多大的I/O???
> Input:
> 1
> 4 6
> dd
> dc
> da
> c
> ab
> aa
> eee aaa eee
>
> Output:
> eee ddd eee
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
bool xiaoyu(string &s1, string &s2, bool shuier[26][26]){
//当s1<s2時,将符合条件的关系填入shuier
int l1 = s1.length(), l2 = s2.length();
for(int i = 0; i < l1 && i < l2; i++){
if(s1[i] != s2[i]){
shuier[s1[i]-'a'][s2[i]-'a'] = 1;
//cout << s1[i] << "<" << s2[i] << endl;
return true;
}
}
if(l1 > l2) return false;
return true;
}
bool dfs_h(bool shuier[26][26], bool used[26], int start, int tar, int A, int *hou){
if(start != tar) (*hou)++;
used[start] = 1;
for(int i = 0; i < A; i++){
if(!used[i] && shuier[start][i]){
if(shuier[i][tar] || !dfs_h(shuier, used, i, tar, A, hou)) return false;
}
}
return true;
}
void dfs_q(bool shuier[26][26], bool used[26], int start, int tar, int A, int *qian){
if(start != tar) (*qian)++;
used[start] = 1;
for(int i = 0; i < A; i++){
if(!used[i] && shuier[i][start]){
dfs_q(shuier, used, i, tar, A, qian);
}
}
}
bool jiancha(bool shuier[26][26], int decrypt[26], int A){
for(int i = 0; i < A; i++) decrypt[i] = -1;
for(int i = 0; i < A; i++){
int qian = 0, hou = 0;
bool used_h[26] = {0};
if(!dfs_h(shuier, used_h, i, i, A, &hou)) return false;
bool used_q[26] = {0};
dfs_q(shuier, used_q, i, i, A, &qian);
if(qian + hou == A-1) decrypt[i] = qian;
}
return true;
}
int main() {
int n;
cin >> n;
for(int ii = 0; ii < n; ii++){
int A,K;
cin >> A >> K;
bool shuier[26][26] = {{0}};
string s1, s2;
bool ky = true;
for(int i = 0; i < K; i++){
if(!ky) {
cin >> s1;
continue;
}
if(i%2==0) cin >> s1;
else cin >> s2;
if(!i) continue;
if(i%2==0) ky = xiaoyu(s2, s1, shuier);
else ky = xiaoyu(s1, s2, shuier);
}
string resStr, s;
while(!s.length()) getline(cin, s);//cin >> s就不行。。。
if(!ky){
cout << "Message cannot be decrypted." << endl;
continue;
}
int decrypt[26];
if(!jiancha(shuier, decrypt, A)){
cout << "Message cannot be decrypted." << endl;
continue;
}
int len = s.length();
for(int i = 0; i < len; i++){
if(s[i] < 'a' || s[i] >= 'a'+A){
resStr += s[i];
}
else if(decrypt[s[i]-'a'] == -1){
ky = false;
break;
}
else{
resStr += (char)('a'+decrypt[s[i]-'a']);
}
}
if(!ky) cout << "Message cannot be decrypted." << endl;
else cout << resStr << endl;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator