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 ...
随机推荐
- EXCLE使用宏生成目录
宏代码: Sub mu() Dim i As Integer Dim ShtCount As Integer Dim SelectionCell As Range ShtCount = Workshe ...
- 通过Maven搭建Mybatis项目
学习通过maven工程搭建Mybatis工程开启对M ybaits的学习总结之旅. 1.首先创建Maven工程. 2.在pom.xml文件中加入依赖的jar <!-- mybatis核心包 -- ...
- [原创] PostgreSQL Plus Advanced Server在Windows中配置双机热备流复制
一.系统环境 操作系统:Windows Server 2003/2008 两个节点分别为master与slave. 主节点master:172.27.19.28 备机点slave:172.27.19. ...
- linux下更改文件夹所属用户和用户组
改变所属用户组:chgrp -R users filename -R是为了递归改变文件夹下的文件和文件夹,users是要改为的用户组名称,filename是要改变的文件夹名称 ============ ...
- jdk 1.6 & 1.7新特性
jdk1.6新特性 1.Desktop类和SystemTray类 2.使用JAXB2来实现对象与XML之间的映射 3.StAX 4.使用Compiler API 5.轻量级Http Server AP ...
- 基础语法 swift
强类型语言:每句代码可以不用分号分隔:大小写敏感: 变量声明: var a = 0 常量声明 let b = 3.14 常量不能+变量?a+b 类型标注 var s :String 打印 pringl ...
- C++设计模式——代理模式
前言 青春总是那样,逝去了才开始回味:大学生活也是在不经意间就溜走了,现在上班的时候,偶尔还会怀念大学时,大家在一起玩游戏的时光.大学喜欢玩游戏,但是可悲的校园网,速度能把人逼疯了:还好,后来搞了一个 ...
- Notes of the scrum meeting(11/4)
meeting time:20:00~20:30p.m.,November 4th,2013 meeting place:20号公寓楼前 attendees: 顾育豪 ...
- C++(MFC)编程中遇到的的一些函数
memset void memset( void dest, int c, size_t count ); dest: Pointer to destination c: Character to s ...
- 反向Ajax,第2部分:WebSocket
转自:http://kb.cnblogs.com/page/112616/ 前言 时至今日,用户期待的是可通过web访问快速.动态的应用.这一文章系列展示了如何使用反向Ajax(Reverse Aja ...