ZOJ3560 Re:the Princess(高斯消元法)
题目要读很久才能理解它的意思和笑点(如果你也看过那个笑话的话),读懂之后就会发现是一个高斯消元法的题目,对于我来说难点不在高斯消元,而在于字符串处理。先来说说题意吧:
总共有n个人,n个人都会有一段话,先是princess说话,里面如果提到了a1,a2,a3...这几个不同的人的话,对应提到的次数是x1,x2,x3..的话,那么下一个对话是ai这个人说的概率是xi/(x1+x2+x3)....,然后下一个人的对话里也会提到别的人,然后也有一定的概率会有下一轮对话,现在要问的就是,给定了这些对话,问你期望的对话次数是多少。
我们可以设第i个人持续的对话的期望是xi,那么xi应该等于 xi=p1*x1+p2*x2+p3*x3+...+1 对应的pi即为在该人对话出现的次数所对应的频率。然后对每个人列出这样的方程就会构成一系列的方程组。然后高斯消元即可。
难度在于算在某个对话里出现了多少次,因为像如何你用KMP去做匹配 prince是会出现在princess那里的,题目说了人名之间是用五种间隔符隔开的,所以匹配的时候或者匹配前将对话里的每一个词分出来,然后和对应的字符串判相等即可。我写的不好跪了好几发呀- -0
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#define ll long long
#define maxn 220
#define eps 1e-7
using namespace std; char str[120][1050];
char name[120][20];
int n; double mat[120][120];
char word[1200]; int dcmp(double x) {
return (x > eps) - (x < -eps);
} int cnt(char*T, char *S)
{
int res = 0; int idx = 0;
for (int i = 0; T[i]; i++){
if (!(T[i] <= 'z'&&T[i] >= 'a')) continue;
int id = 0;
while (T[i] <= 'z'&&T[i] >= 'a'){
word[id++] = T[i++];
}
word[id] = '\0';
if (strcmp(word, S) == 0) res++;
i--;
}
return res;
} bool gauss(double mat[120][120], int n)
{
for (int i = 0; i < n; i++){
int pivot = i;
for (int j = i; j < n; j++){
if (abs(mat[j][i])>abs(mat[pivot][i])) pivot = j;
}
swap(mat[i], mat[pivot]);
if (abs(mat[i][i]) < eps) return false;
for (int j = i + 1; j <= n; j++) mat[i][j] /= mat[i][i];
for (int j = 0; j < n; j++){
if (i != j)
for (int k = i + 1; k <= n; k++){
mat[j][k] -= mat[j][i] * mat[i][k];
}
}
}
return true;
} int main()
{
while (cin >> n)
{
getchar();
memset(name, 0, sizeof(name));
memset(str, 0, sizeof(str));
for (int i = 0; i < n; i++){
gets(str[i]);
int j;
for (j = 0; str[i][j]; j++){
if (str[i][j] == ':') break;
name[i][j] = str[i][j];
}
name[i][j + 1] = '\0'; int k = 0; j++;
for (; str[i][j]; j++,k++){
str[i][k] = str[i][j];
}
str[i][k] = '\0';
}
memset(mat, 0, sizeof(mat));
for (int i = 0; i < n; i++){
double tot = 0; double tmp = 0;
for (int j = 0; j < n; j++){
if (i == j) continue;
tmp = cnt(str[i], name[j]);
mat[i][j] = tmp;
tot += tmp;
}
mat[i][i] = -tot; mat[i][n] = -tot;
if (dcmp(tot) == 0){
mat[i][i] = mat[i][n] = -1;
}
}
if (gauss(mat, n)){
printf("%.3lf\n", mat[0][n]);
}
else puts("Infinity");
}
return 0;
}
ZOJ3560 Re:the Princess(高斯消元法)的更多相关文章
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
- HDU--杭电--1026--Ignatius and the Princess I--广搜--直接暴力0MS,优先队列的一边站
别人都是广搜+优先队列,我没空临时学,所以就直接自己暴力了 Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1026(Ignatius and the Princess I)BFS
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- hdu acm 1028 数字拆分Ignatius and the Princess III
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hdu1026.Ignatius and the Princess I(bfs + 优先队列)
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- sdut 2603:Rescue The Princess(第四届山东省省赛原题,计算几何,向量旋转 + 向量交点)
Rescue The Princess Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 Several days ago, a b ...
- hdu 1029 Ignatius ans the Princess IV
Ignatius and the Princess IV Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32767 K ( ...
- POJ 1222 (开关问题+高斯消元法)
题目链接: http://poj.org/problem?id=1222 题目大意:一堆开关,或开或关.每个开关按下后,周围4个方向开关反转.问使最后所有开关都关闭的,开关按法.0表示不按,1表示按. ...
- ACM: HDU 1028 Ignatius and the Princess III-DP
HDU 1028 Ignatius and the Princess III Time Limit:1000MS Memory Limit:32768KB 64bit IO Form ...
随机推荐
- wamp环境搭建
php 进入php安装目录. 修改php.ini-developement为php.ini 配置扩展目录为你的PHP安装目录下的ext目录,在我这里是 extension_dir = "C: ...
- App.config的学习笔记
昨天基本弄清config的使用之后,再看WP的API,晕了.结果WP不支持system.configuration命名空间,这意味着想在WP上用App.config不大可能了. WP具体支持API请查 ...
- ED/EP系列3《基本指令》
Ø --APPLICATIONBLOCK(应用锁定): Ø --APPLICATION UNBLOCK(应用解锁): Ø --CARDBLOCK(卡片锁定): Ø --EXTERNAL AUTHENT ...
- eclipse java 空心J文件的回复
eclipse中的空心J的java文件,表示当前文件不包含在项目中进行编译,而仅仅是当做资源存在项目中. 解决方案如下: 1.鼠标右击当前空心j文件,-->build path-->inc ...
- Oracle private dblink和pubic dblink
DB : 11.2.0.3.0 Oracle DBLINK 创建分为private 和 public dblink,默认创建的为private ; private dblink 只有创建的schema ...
- hdu 1874 畅通工程续
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...
- iOS开发多线程篇—单例模式(ARC)
iOS开发多线程篇—单例模式(ARC) 一.简单说明: 设计模式:多年软件开发,总结出来的一套经验.方法和工具 java中有23种设计模式,在ios中最常用的是单例模式和代理模式. 二.单例模式说明 ...
- Win7下的本地网站发布
今天闲来无事研究了一下网站的发布,之前一直以为很难的样子,当真正实现了就觉得他也不过如此,现在来把我的研究结果分享一下,如果有问题望大家提出来! 首先发布网站我们要在本地的电脑上安装IIS,这个就不多 ...
- Qt的Qss样式
http://www.cnblogs.com/coffeegg/archive/2011/11/15/2249452.html(转) http://blog.csdn.net/cgzhello1/ar ...
- Linux Shel高级技巧(目录)
Linux Shell高级技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/12/22/2271167.html一.将输入信息转换为大写字 ...